社区
C语言
帖子详情
这个宏应该怎么写
mcawxd
2011-10-17 01:56:00
想定义一个类似于这样的宏
#define LOG __FILE__##hashcode
如果在c:\comAPI.cpp引用这个宏得到的结果将是 c:\comAPI.cpphashcode,我想得到的结果是comhashcode,就是把路径和.cpp等无用的字符去掉,得到的这个字符又作为一个新的宏,如:
#define comhashcode 0x123
请问这个LOG宏应该怎么写。
...全文
136
11
打赏
收藏
这个宏应该怎么写
想定义一个类似于这样的宏 #define LOG __FILE__##hashcode 如果在c:\comAPI.cpp引用这个宏得到的结果将是 c:\comAPI.cpphashcode,我想得到的结果是comhashcode,就是把路径和.cpp等无用的字符去掉,得到的这个字符又作为一个新的宏,如: #define comhashcode 0x123 请问这个LOG宏应该怎么写。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
宏
的理解:怎么
写
多行的
宏
本文探讨了在C语言中如何使用
宏
来实现多行代码的功能,通过实例演示了如何编
写
一个swap函数的
宏
,并强调了在
宏
定义中正确使用续行符及避免在续行符后添加注释的重要性。
写
宏
注意
本文通过实例演示了在C++中使用
宏
定义时可能遇到的问题,特别是传递自增表达式到
宏
中的错误做法及未正确使用括号导致的运算符优先级问题。
汇编——
宏
应该
怎么
写
本文详细介绍了汇编语言中的
宏
指令,包括
宏
的定义、调用方式及其与子程序的区别。通过实例讲解
宏
如何简化编程,提高代码复用率。
如何把鼠标
宏
用c语言
写
出来,鼠标
宏
怎么设置,手把手教你鼠标如何设置
宏
本文详细介绍了如何为不同类型的鼠标设置
宏
,包括选择合适的游戏鼠标和对应驱动,自定义
宏
定义,录制和应用
宏
的过程,让你在游戏中如虎添翼。
clojure
宏
写
宏
本文通过具体的示例对比了Common Lisp与Clojure中
宏
的实现方式,并讨论了两者之间的差异。Common Lisp的
宏
虽然功能强大但也可能带来副作用,而Clojure的
宏
则更加安全但在某些场景下显得有些限制。
C语言
70,038
社区成员
243,247
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章