暧昧什么意思| 下面流出发黄的液体是什么原因| amass是什么牌子| 百合花什么时候种植| 挂科什么意思| 冷感冒吃什么药好得快| 什么的狼| 十月十日是什么星座| 种什么药材最快又值钱| 什么是a货翡翠| 以梦为马是什么意思| 蚝油可以用什么代替| 深海鱼油的作用是什么| ca19-9偏高是什么意思| 包臀裙配什么上衣| 1992年属猴的是什么命| 肖像是什么意思| 耳鸣和脑鸣有什么区别| 失眠用什么药| 阴道炎症是什么症状| 胬肉是什么| angry是什么意思| 十月十七是什么星座| 灵芝和什么煲汤好| pick什么意思| 茶毫是什么| 什么叫尿潜血| 启蒙是什么意思| 吃什么伤口愈合的快| 早上醒来手麻是什么原因| fsh是什么激素| 绿豆可以和什么一起煮| 丢是什么意思| 罄竹难书是什么意思| 小孩拉肚子应该吃什么食物好| 锁阳是什么| 牛宝是什么| 鸡与什么生肖相合| 什么是外包| 40岁属什么生肖| 认知是什么| 痛风要吃什么药好得快| 什么是丹毒| 绞丝旁一个奇念什么| 什么的高| 孩子为什么有胎记| 甲状腺手术后有什么后遗症| 什么茶解酒| 天秤座是什么星象| 人为什么会觉得累| 腰疼不能弯腰是什么原因引起的| 天时地利人和是什么意思| 昏昏欲睡是什么意思| 牙龈肿胀吃什么药| 拉黑色的屎是什么原因| 鬼子红药店里叫什么药| 胸片是什么| 红薯不能和什么一起吃| 白事的礼金叫什么| 12月15号是什么星座| 端午节喝什么酒| bv中间型是什么意思| 身主天机是什么意思| 好马不吃回头草是什么意思| 胆囊息肉有什么症状| 为什么叫拉丁美洲| 大便很臭什么原因| 牙龈肿痛发炎吃什么药| 命中劫是什么意思| 圆脸适合什么镜框| 靛青色是什么颜色| mrmrs是什么牌子| 淋巴排毒是什么意思| 螺蛳粉是什么做的| 尿检是检查什么的| 丝瓜水敷脸有什么作用| 萌萌哒是什么意思| 肌红蛋白高是什么原因| 冬虫夏草到底是什么| 牛仔裙配什么上衣好看| 飞蛾吃什么东西| 什么是肩袖损伤| longines是什么牌子| 甄嬛传什么时候上映的| 湿疹涂什么药| 为什么贫穷| 尿中有泡沫是什么原因| 转氨酶高说明什么| 过敏痒用什么药膏| 宜是什么意思| 什么的池水| 晚上老咳嗽是什么原因| 腺肌症有什么症状表现| 什么原因引起高血压| 2003年是什么命| 花木兰是什么朝代| 地位是什么意思| 早上六七点是什么时辰| 木薯是什么| 为什么大熊猫是国宝| 先自度其足的度是什么意思| 遇难呈祥是什么生肖| 女性得疱疹是什么症状| 蹲久了站起来头晕是什么原因| 肠胃炎需要做什么检查| 精神卫生科看什么病| instagram是什么软件| 猫吃什么会死| 什么是附件炎| 夏对什么| l1是什么意思| 菌群失调是什么意思| 炖大骨头放什么调料| 6月18日是什么节| 肠胃蠕动慢吃什么药| 两个马念什么| 一直想大便是什么原因| 吃什么药能快速降血压| 年糕是什么做的| 猪脚煲汤放什么材料好| 胃酸分泌过多吃什么药| 老人尿失禁吃什么药最好| 阿魏是什么中药| 一什么图画| 什么是汛期| 燃气灶什么品牌好| 傻瓜是什么意思| 感谢是什么意思| 宿便什么意思| 芽原基发育成什么| 007什么意思| 四面八方什么生肖| 肚子疼挂什么科| 什么人容易得阿尔兹海默症| 睾丸长什么样| 中暑为什么不能打点滴| 甲亢食疗吃什么| 喉咙嘶哑是什么原因| rh血型D阳性是什么意思| 想吃甜食是身体缺什么| 胃隐隐作痛吃什么药| 特殊情况是什么意思| 补钙吃什么最好| 武松的绰号是什么| 子宫饱满是什么意思| 吃什么东西可以变白| 额头上长痘痘是什么原因| 大拇指旁边是什么指| 月经为什么是黑色的| 男的纹般若有什么寓意| 九月什么星座| 甘油是什么东西| 什么叫免疫力| 冰箱买什么牌子好| 为什么会有头皮屑| 胃泌素瘤是什么意思| 端坐呼吸常见于什么病| 人中深浅代表什么| 睡觉憋气是什么原因引起的| 尿酸高吃什么肉| 中央候补委员是什么级别| 脊柱侧弯有什么危害| 新生儿老是打嗝是什么原因| 老是觉得口渴是什么原因引起的| 宛字五行属什么| 口腔出血是什么病征兆| 失业是什么意思| 捡肥皂是什么意思| 国务院秘书长什么级别| 小囊性灶是什么意思| 白带像豆腐渣用什么药| 梦见建房子是什么预兆| 吃的少还胖什么原因| 珍惜眼前人是什么意思| 绩效工资是什么| 什么是npc| 秋葵有什么作用| 嗓子肿痛吃什么药| 来年是什么意思| 92什么意思| 肝功能七项是检查什么| 为什么头晕晕乎乎的| 喜爱的反义词是什么| 肠化十是什么意思| 梦见打狼是什么预兆| 国家穷为什么不多印钱| 拉血是什么原因| 狗刨坑是什么征兆| 肝脏b超能检查出什么| 西药是什么药| 八府巡按是什么官| 扁平息肉属于什么性质| 中风什么症状| 咖啡色配什么颜色好看| 儿童肠炎吃什么药| 佐匹克隆片是什么药| 陕西的特产有什么| 什么木头做菜板好| 阳气是什么意思| 脂肪垫是什么| 个人solo什么意思| dq是什么| 宫腔粘连是什么原因引起的| buffalo是什么牌子| 为什么睡觉流口水很臭| 尿毒症是什么原因导致的| lcr是什么意思| 戌是什么意思| 打日本电话前面加什么| 椰子煲鸡汤放什么材料| 11.6号是什么星座| 什么的豆角| 十一月十一号是什么星座| 复试是什么意思| 冰字五行属什么| b-h是什么药| 迟脉是什么意思| 男人右眼皮跳是什么预兆| 来例假肚子疼是什么原因| 电子商务有限公司是做什么的| 大名是什么意思| 冲猴煞北是什么意思| 甜菜根是什么菜| 早搏吃什么药最管用| 什么可以醒酒| 吃什么补黑色素最快| 没出息什么意思| 农历五月二十是什么星座| 特长有什么| 茉莉花什么时候开花| 孕妇血糖高可以吃什么水果| 电子商务有限公司是做什么的| 瑞什么意思| 奇亚籽是什么| 羊奶和牛奶有什么区别| 插茱萸是什么意思| 肝胆胰脾挂什么科| aape是什么牌子| 蜜饯是什么意思| 葛根是什么东西| 手不自主颤抖是什么病| 腿毛旺盛是什么原因| 吃什么东西对心脏好| 蓝色搭配什么颜色| 北京大学什么专业最好| 海肠是什么东西| 人体电解质是什么| 沂字五行属什么| 契爷是什么意思| 遇人不淑什么意思| 肺腺瘤是什么| 增加胃动力最好的药是什么药| 丙申五行属什么| 秸秆是什么| 脚上长水泡是什么原因引起的| 一月出生是什么星座| 解酒喝什么好| 风热感冒吃什么药| 补充胶原蛋白吃什么最好| 身经百战是什么意思| 为什么午睡起来会头疼| 白雪什么什么| 支气管舒张试验阳性是什么意思| 影字五行属什么| 百度
The Database Managers, Inc.

Contact The Database Managers, Inc.


Use an RSS enabled news reader to read these articles.Use an RSS enabled news reader to read these articles.

西樵山欲打造粤港澳大湾区文旅大IP

by Curtis Krauskopf
百度 不信?那你不妨回忆一下,历史上哪一代的凯美瑞有这样激进的设计?这种希望在车流中吸睛的张扬彻底站在了过去那种低调做人高调做事的对立面上了。

Q:  How can I create a string that contains the C++ filename and line number that a runtime error occurs on?

A:   The __FILE__ C++ preprocessor directive contains the name of the file being compiled. Similarly, the __LINE__ preprocessor directive contains the line number of the original source file that is being compiled. Both the __FILE__ and __LINE__ preprocessor directives have two underscores both before and after the word. Separating each character of the __FILE__ preprocessor directive looks like:

_ _ F I L E _ _

Listing 1 is a console-mode application that shows an easy way to associate runtime errors with the original source line.

#include <stdio.h>

int main(int , char**)
{
  printf("This fake error is in %s on line %d\n",
__FILE__, __LINE__); return 0; }
Listing 1: Show the Location of a Runtime Error

Figure 1 shows the result of running the program in Listing 1:

This fake error is in c:\temp\test.cpp on line 5
Figure 1: The Output of Listing 1

Taking It One Step Further

I want every error report to go through a common error() function so that I can set breakpoints when certain errors occur and so I can segregate how errors are handled. For example, I might want some errors to appear on the screen and other errors to be logged to a file. And some errors might need to appear on the screen and be logged to a file.

A prototype for such an error logging function is:

void error(const char *file, const unsigned long line,
const char *msg); 

and it would be called like this:

error(__FILE__, __LINE__, "my error message"); 

Preprocessor Magic

There are three awkward parts to the above solution:

  1. The __FILE__ and __LINE__ preprocessor directives need to be added to every error() function call.
  2. It's easy to forget to put both underscores on both parts of the __FILE__ and __LINE__ directives. Getting it wrong will lead to a compile-time error.
  3. __LINE__ is an integer. Doing string manipulation on an integer just adds another level of complexity to any error() function I create. I will never need to use __LINE__ as an integer -- I always want to use it as a string so that it can be output to the screen or a log file.

It would be nicer if __FILE__ and __LINE__ could somehow be handled automatically so I couldn't get them wrong every time I write an error() call.

What I would like to be able to do is write something like:

error(AT, "my error message"); 

In the above example, the AT macro would expand to be "c:\temp\test.cpp:5".

The prototype for my new error() function becomes:

void error(const char *location, const char *msg); 

Because the Borland C++ Builder compiler automatically merges adjacent strings, I can create a #define for AT that looks like this:

#define AT __FILE__ ":" __LINE__ 

That doesn't work, though, because __LINE__ expands to an integer. The above #define expands to this at compile-time:

"c:\temp\test.cpp" ":" 5

That is an invalid string because strings can't have an unquoted integer at the end of the string.

A special preprocessor directive that turns a symbol into a string is the # token. Changing the above #define to

#define AT __FILE__ ":" #__LINE__ 

seems like it should work but it doesn't because the compiler complains that # is an illegal character. The problem is that the # preprocessor symbol is only recognized when it's used like this:

#define symbol(X) #X 

So, not being one to fight the problem, I'll change my AT macro to look like this:

#define STRINGIFY(x) #x
#define AT __FILE__ ":" STRINGIFY(__LINE__) 

That compiles, but at runtime it yields the bizarre message in Figure 2:

c:\temp\test.cpp:__LINE__: fake error
Figure 2: The preprocessor directive appears in the output.

As shown in Figure 2, the __LINE__ preprocessor directive itself has become a part of the output!

The solution is to take the STRINGIFY() solution one step further -- to wrap the STRINGIFY() macro in yet another macro:

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)

Listing 2 shows the final sample program and Figure 3 shows the output of Listing 2.

#include <stdio.h>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)
void error(const char *location, const char *msg)
{
  printf("Error at %s: %s\n", location, msg);
}
int main(int , char**)
{
  error(AT, "fake error");
  return 0;
}
Listing 2: The final solution that turns __LINE__ into a string

Error at c:\temp\test\test.cpp:11: fake error
Figure 3: The Output of Listing 2

Visual Studio Support

Tim Johnston tried this solution in Microsoft Video Studio but he found that the __LINE__ preprocessor symbol did not have the correct value in debug mode. His solution was to change the "Debug Information Format" setting on the C/C++ project setting tab. The setting that does not work is "Program Database for Edit and Continue"; the setting that works is "Program Database".

Conclusion:

The preprocessor directives __FILE__ and __LINE__ can provide some useful debugging information. This information can be made available at runtime by print()ing those values to the screen or to a log file.

Transforming the __LINE__ preprocessor directive into a string turned out to be much more difficult than originally imagined. Through the use of some #define preprocessor magic, though, the __LINE__ preprocessor directive was tamed and forced to compile as a string.

This has the advantage that the string is automatically merged with the __FILE__ preprocessor value which creates one string for error processing. This also has the advantage of removing the need for integer to string conversion in the error() function.


This article was written by Curtis Krauskopf (email at ).

Curtis Krauskopf is a software engineer and the president of The Database Managers (www.decompile.com). He has been writing code professionally for over 25 years. His prior projects include multiple web e-commerce applications, decompilers for the DataFlex language, aircraft simulators, an automated Y2K conversion program for over 3,000,000 compiled DataFlex programs, and inventory control projects. Curtis has spoken at many domestic and international DataFlex developer conferences and has been published in FlexLines Online, JavaPro Magazine, C/C++ Users Journal and C++ Builder Developer's Journal.


Popular C++ topics at The Database Managers:

The Database Managers helps companies to:
  •  become more profitable
  •  grow their business
  •  fix programs that are behaving badly
  •  write new programs to solve business problems
  • do more with fewer resources
Email them at to find out how to make your company more successful.
C++ FAQ Services | Programming | Contact Us | Recent Updates
Send feedback to:
子宫低回声结节是什么意思 侧切是什么意思 紫萱名字的含义是什么 氟比洛芬是什么药 高晓松为什么进监狱
什么是阴道炎 鸡犬不宁是什么生肖 结节灶是什么意思啊 送终是什么意思 14时是什么时辰
青蛙靠什么呼吸 身上麻是什么原因 天蓝色是什么颜色 柠檬加蜂蜜泡水喝有什么功效 41年属什么生肖
土耳其是什么人种 动脉抽血为什么这么疼 处暑是什么意思 梦见考试是什么意思 阴道炎用什么栓剂
胡思乱想是什么意思hcv8jop2ns3r.cn 宝宝大便绿色是什么原因hcv9jop2ns8r.cn 唱过什么歌hcv8jop3ns9r.cn 吃什么对大脑记忆力好hcv8jop8ns2r.cn 骆驼奶有什么功效hcv8jop9ns0r.cn
贵州菜属于什么菜系hcv8jop5ns7r.cn 四月27日是什么星座hcv9jop3ns2r.cn susie是什么意思hcv9jop3ns2r.cn john是什么意思hcv7jop6ns9r.cn 早射吃什么药hcv7jop9ns0r.cn
杨枝甘露是什么做的hcv9jop0ns9r.cn 惊悉是什么意思hcv8jop6ns2r.cn qd是什么意思jingluanji.com 什么是乙肝病毒携带者hcv8jop8ns0r.cn 海带不能和什么一起吃96micro.com
什么是abo文hcv9jop0ns5r.cn 苗字五行属什么hcv8jop6ns7r.cn 搭桥香是什么意思hcv9jop6ns0r.cn 肌无力有什么症状hcv9jop4ns7r.cn 中国红是什么颜色hcv9jop1ns7r.cn
百度