症状描述:
用Eclipse调试程序,执行printf和cout函数,但是console无内容显示。
原因分析:
Eclipse输出的内容是保存在buffer中,因此要显示相关内容,就必须刷buffer缓冲区。
解决方案:
1.在main函数开始时调用函数 setbuf(stdout,NULL);
2.在每个printf函数后调用函数 fflush(stdout);
int main(void) {
setbuf(stdout, NULL);
char* c="!!!Hello C!!!";
printf(c); /* prints !!!Hello World!!! */
//fflush(stdout);
return EXIT_SUCCESS;
}
字符串c结尾没加\n,调试时报以下错误:
!!!Hello C!!!*stopped,reason="end-stepping-range",frame={addr="0x0040140f",func="main",args=[],file="..\src\HelloC.c",fullname="F:\\316\322\265\304\316\304\265\265\Workspaces\HelloC\Debug/..\src\HelloC.c",line="24"},thread-id="1",stopped-threads="all"
加上\n就好了。
int main(void) {
setbuf(stdout, NULL);
char* c="!!!Hello C!!!\n";
printf(c); /* prints !!!Hello World!!! */
//fflush(stdout);
return EXIT_SUCCESS;
}
这是另一篇博客:
症状描述:使用Eclipse运行或者调试C/C++应用程序时,printf和cout调用无法实时输出到控制台
原因分析:当程序在Eclipse中运行时,默认把输出输出关联到了Eclipse的console,而CDT的console是带输出缓存的,所以会出现printf/cout调用时控制台(console)无输出情况,事实上是这个输出没有被实时输出到console上。
解决方案:
1, 代码全局设置:在main函数入口处设置stdout和stderr的缓存区为NULL,即调用 setbuf(stdout, NULL) 和 setbuf(stderr, NULL)
2, 代码局部设置:在每个printf/cout类似输出的地方调用fflush(stdout)或者fflush(stderr)
3,CDT全局设置:找半天没找到设置的地方:(,如果你知道的话,麻烦告诉我啊:)