这个宏应该怎么写

mcawxd 2011-10-17 01:56:00
想定义一个类似于这样的宏
#define LOG __FILE__##hashcode
如果在c:\comAPI.cpp引用这个宏得到的结果将是 c:\comAPI.cpphashcode,我想得到的结果是comhashcode,就是把路径和.cpp等无用的字符去掉,得到的这个字符又作为一个新的宏,如:
#define comhashcode 0x123

请问这个LOG宏应该怎么写。
...全文
115 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
东莞某某某 2011-10-17
  • 打赏
  • 举报
回复
写一个宏函数 从__FILE__中抽取名字
mcawxd 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luciferisnotsatan 的回复:]
那就只能你自己写了。
#define LOG(FILENAME) FILENAME##hashcode
[/Quote]
我有很多cpp文件都会引用这个宏。来得到当前文件名并加一个hashcode,目的是为了在另一个头文件中得到对应的数值。如:comAPI.cpp使用这个宏时得到的是 0x123,com1API使用时得到的是0x456。
#define comhashcode 0x123
#define com1hashcode 0x456
crystal_avast 2011-10-17
  • 打赏
  • 举报
回复
修改后的得文件名方法:
LPCTSTR GetFileName()
{
CString csFilePath( _T( __FILE__ ));
int nPos = csFilePath.Find( _T( "." ), 0 );
CString csSubFilePath = csFilePath.Mid( 0, nPos );
int nStartPos = 0;
int nFindPos = csSubFilePath.Find( _T( "\\" ), nStartPos );
static CString csFileName;
while ( TRUE )
{
if ( nFindPos == -1 )
{
csFileName = csSubFilePath.Mid( nStartPos + 1 );
break;
}
nStartPos = nFindPos;
nFindPos = csSubFilePath.Find( _T( "\\" ), nStartPos + 1 );
}

return csFileName.operator LPCTSTR();
}
crystal_avast 2011-10-17
  • 打赏
  • 举报
回复
LPCTSTR GetFileName()
{
CString csFilePath( _T( __FILE__ ));
int nPos = csFilePath.Find( _T( "." ), 0 );
CString csSubFilePath = csFilePath.Mid( 0, nPos );
int nStartPos = 0;
int nFindPos = csSubFilePath.Find( _T( "\\" ), nStartPos );
static CString csFileName;
while ( TRUE )
{
nStartPos = nFindPos;
nFindPos = csSubFilePath.Find( _T( "\\" ), nStartPos + 1 );
if ( nFindPos == -1 )
{
csFileName = csSubFilePath.Mid( nStartPos + 1 );
break;
}
}

return csFileName.operator LPCTSTR();
}
crystal_avast 2011-10-17
  • 打赏
  • 举报
回复
我写了一个得到文件名的函数,是用CString实现的,或许对LZ有用

LPCTSTR GetFileName()
{
CString csFilePath( _T( __FILE__ ));
int nPos = csFilePath.Find( _T( "." ), 0 );
CString csSubFilePath = csFilePath.Mid( 0, nPos );
int nStartPos = 0;
int nFindPos = csSubFilePath.Find( _T( "\\" ), nStartPos );
static CString csFileName;
while ( TRUE )
{
nStartPos = nFindPos;
nFindPos = csSubFilePath.Find( _T( "\\" ), nStartPos + 1 );
if ( nFindPos == -1 )
{
csFileName = csSubFilePath.Mid( nStartPos + 1 );
break;
}
}

return csFileName.operator LPCTSTR();
}

赵4老师 2011-10-17
  • 打赏
  • 举报
回复
在c:\com*.cpp每个文件第一行加
#define LOG com##hashcode
lk1928 2011-10-17
  • 打赏
  • 举报
回复
引用 1 楼 luciferisnotsatan 的回复:]
那就只能你自己写了。
#define LOG(FILENAME) FILENAME##hashcode
[/Quote]
柯本 2011-10-17
  • 打赏
  • 举报
回复
另:
像这种需求就不会用宏了,在程序中实现吧
柯本 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]
引用 1 楼 luciferisnotsatan 的回复:
那就只能你自己写了。
#define LOG(FILENAME) FILENAME##hashcode

+1
[/Quote]
++
bdmh 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luciferisnotsatan 的回复:]
那就只能你自己写了。
#define LOG(FILENAME) FILENAME##hashcode
[/Quote]
+1
luciferisnotsatan 2011-10-17
  • 打赏
  • 举报
回复
那就只能你自己写了。
#define LOG(FILENAME) FILENAME##hashcode

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧