Eclipse - 在调用printf()后,将fflush(stdout)设置为默认值 [英] Eclipse - make the fflush(stdout) as default after printf() calling
问题描述
有同一案例。
是否有任何解决方案使 fflush(stdout)
会在 printf()
而不是在每个 printf()
调用之后添加 fflush(stdout)
我为C / C ++开发人员使用 Eclipse IDE 和
gcc --version gcc GCC)4.8.1 on
windows 7
如果您想全局禁用缓冲,您可以使用 setvbuf
:
setvbuf(stdout,NULL,_IONBF,0);
在您的程式开始处。
如果您只想为某些调用执行此操作,您可以定义自己的宏来执行此操作,例如:
#define printflush(s,...)do {printf(s,__VA_ARGS__); fflush(stdout中); } while(0)
它将这两个函数调用放在一个新的范围内, p>
在这两种情况下,您至少需要两个参数(如 printflush(id =%d \ n,id) code>),否则在编译时会导致语法错误。
上述问题的GCC特定解决方案:您可以扩展上面的宏,以便它也可以使用单个参数:
#define printflush(s,...)do { printf(s,## __ VA_ARGS__); fflush(stdout中); } while(0)
这样,您可以在 printflush中使用它(嘿!)
。
编辑:正如@unwind所指出的,可变参数宏已经标准化在C99中。不过,GCC 4.8会理解他们,不需要任何额外的开关。
Having same case.
Is there any solution such that the fflush(stdout)
will occur automatically after printf()
instead of add fflush(stdout)
after each printf()
calling ?
I using Eclipse IDE for C/C++ Developers
and gcc --version gcc (GCC) 4.8.1
on windows 7
If you want to disable buffering globally, you can use setvbuf
:
setvbuf(stdout, NULL, _IONBF, 0);
at the beginning of your program.
If you want to do it only for some calls, you can define your own macro to do so, like:
#define printflush(s, ...) do { printf(s, __VA_ARGS__); fflush(stdout); } while (0)
which puts the two function calls inside a new scope with a trick.
In both cases, you will need to have at least two arguments (like printflush("id = %d\n", id)
), or you will cause a syntax error at compile time.
GCC specific solution to the problem above: you can extend the macro above so that it works with a single parameter too:
#define printflush(s, ...) do { printf(s, ##__VA_ARGS__); fflush(stdout); } while (0)
This way, you can use it also with printflush("Hey!")
.
EDIT: as pointed out by @unwind, variadic macros have been standardized in C99. Still, GCC 4.8 will understand them without any extra switch.
这篇关于Eclipse - 在调用printf()后,将fflush(stdout)设置为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!