社区
C语言
帖子详情
这个宏应该怎么写
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
打赏
收藏
这个宏应该怎么写
想定义一个类似于这样的宏 #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
写
出一个标准
宏
,这个
宏
输入两个参数并返回较小的一个
写
出一个标准
宏
,这个
宏
输入两个参数并返回较小的一个 #define MIN(X,Y) ((X)>(Y)?(Y):(X))
每日一题(9)——
写
一个标准
宏
MIN,这个
宏
输入两个参数并返回较小的一个
写
一个标准
宏
MIN,这个
宏
输入两个参数并返回较小的一个。 分析:
宏
定义的注意两点: 1、数据类型是否有溢出的风险(如一年有多少个秒的
宏
); 2、带参
宏
的每个参数都要用括号括起来。 #define MIN(x,y) ((x) ...
写
一个“标准“
宏
MIN ,这个
宏
输入两个参数并返回较小的一个
面试题目:
写
一个”标准"
宏
MIN ,这个
宏
输入两个参数并返回较小的一个 答: 这个测试是为下面的目的而设的: 1) 标识#define在
宏
中应用的基本知识,这是一个非常重要的基础知识。因为在嵌入(inline)操作符变为...
写
一个“标准”
宏
MIN,这个
宏
输入两个参数并返回较小的一个?
写
一个“标准”
宏
MIN,这个
宏
输入两个参数并返回较小的一个?-->#define MIN(X,Y) ((X)>(Y)?(Y):(X))结果分析:define只会是纯替换作用,所以X,Y均需要加括号,以防止X,Y为表达式的情况...
汇编——
宏
应该
怎么
写
什么是
宏
C语言
69,369
社区成员
243,082
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章