复杂的宏... [英] Complicated macro...

查看:57
本文介绍了复杂的宏...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C程序的情况,其中包含数百个调用

" fprintf(stdout,...",我需要立即刷新stdout
$在任何流发送到那里之后b $ b。更具体地说,我需要在每个以下开头的命令后添加

命令fflush(stdout);


fprintf(stdout

fprintf(stdout

fprintf(stdout

fprintf(stdout

fprintf (stdout




我手动完成此操作(大约需要30分钟),但我后来认为这是
我本可以定义一个宏来处理这个

替换。但是,我甚至无法想到处理每个变长fprintf所需的定义

打电话。


有什么想法吗?


谢谢。

I had a situation in a C program, which contained hundreds of calls to
"fprintf (stdout, ...", where I needed to flush stdout immediately
after any stream was sent there. More specifically, I needed to add the
command "fflush(stdout);" after every command that started with:

fprintf(stdout
fprintf (stdout
fprintf( stdout
fprintf ( stdout
fprintf ( stdout
etc.

I wound up doing this manually (took about 30 minutes), but I later
thought that I could have defined a macro to take care of this
substitution. However, I couldn''t even begin to think of the definition
needed to handle every variable length fprintf call.

Any ideas?

Thanks.

推荐答案

No Such Luck写道:
No Such Luck wrote:
我遇到了一个C p的情况rogram,包含数百次调用
到fprintf(stdout,...,我需要在任何流发送后立即刷新stdout
。更具体地说,我需要在命令fflush(stdout);中添加
。在每个以下开头的命令之后:
I had a situation in a C program, which contained hundreds of calls to "fprintf (stdout, ...", where I needed to flush stdout immediately
after any stream was sent there. More specifically, I needed to add the command "fflush(stdout);" after every command that started with:




您是否考虑使用

C标准库中的setvbuf()函数来关闭缓冲stdout?

http:/ /www.opengroup.org/onlinepubs/...h/setvbuf.html


通常stdout在每一行之后或当下一次输入时都会消失

请求并且没有必要冲洗..


但是如果你每次只是冲洗,看起来你可能也好吗

只是暂停了缓冲


-J。 Hess



Did you consider using the setvbuf() function from the
C standard library to just turn off buffering of stdout?

http://www.opengroup.org/onlinepubs/...h/setvbuf.html

Usually stdout goes out after every line or when input is next
requested and there''s little need to flush..

But if you just flush every time, it seems like you might as well
just have the buffering turned off

-J. Hess


>您是否考虑使用
> Did you consider using the setvbuf() function from the
C标准库中的setvbuf()函数来关闭stdout的缓冲?

http://www.opengroup.org/onlinepubs/...h/setvbuf.html

我不知道这个,看起来它可能是一个可接受的解决方案。出于好奇,为什么stdout缓冲

的默认行为?我无法想到为什么缓冲

stdout需要的原因很多。

通常stdout在每一行之后或当下一次输入时都会消失
请求并且没有必要冲洗..


我同意。在命令行模式下,这是看到的行为。然而,

然后这个程序被称为子进程,并且用于
的管道捕获进程的stdout,有相当大的延迟。

在这种情况下需要刷新。

但是如果你每次都冲洗,看起来好像你也可以关闭缓冲
C standard library to just turn off buffering of stdout?

http://www.opengroup.org/onlinepubs/...h/setvbuf.html
I did not know about this, and it seems like it would have been an
acceptable solution. Just out of curiousity, why is stdout buffering
the default behavior? I can''t think of that many reasons why buffering
of stdout would needed anyway.
Usually stdout goes out after every line or when input is next
requested and there''s little need to flush..
I agree. And in command line mode, this is the behavior seen. However,
then this program is called as a child process, and a pipe used to
capture the stdout of the process, there is a considerable delay.
Flushing is necessary, in this case.
But if you just flush every time, it seems like you might as well
just have the buffering turned off




我同意。我仍然对可能的宏定义解决方案感兴趣,




谢谢!



I agree. I''m still interested in a possible macro definition solution,
as well.

Thanks!




No Such Luck写道:

No Such Luck wrote:
我有一个C程序的情况,其中包含数百个调用
到fprintf (stdout,...,我需要在任何流发送后立即刷新stdout
。更具体地说,我需要在命令fflush(stdout);之后添加
每个以下开头的命令:

fprintf(stdout
I had a situation in a C program, which contained hundreds of calls to "fprintf (stdout, ...", where I needed to flush stdout immediately
after any stream was sent there. More specifically, I needed to add the command "fflush(stdout);" after every command that started with:

fprintf(stdout




你熟悉可变参数宏吗?


在C99你可以做类似的事情


#define print_it(file_ptr,format,...)do \

if(fprintf(file_ptr,格式,## __ VA_ARGS__)== 0)

fflush(file_ptr);

/ * else ...如果你喜欢* /
$处理错误b $ b while(0)



Are you familiar with variadic macros?

In C99 you could do something like

#define print_it(file_ptr, format, ...) do \
if (fprintf(file_ptr, format, ##__VA_ARGS__ ) == 0)
fflush(file_ptr);
/* else... handle the error if you like */
while(0)


这篇关于复杂的宏...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆