473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Complicated macro...

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.

Nov 14 '05 #1
11 3345
No Such Luck wrote:
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:


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

Nov 14 '05 #2
> 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
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!

Nov 14 '05 #3

No Such Luck wrote:
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


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)

Nov 14 '05 #4
James Hess wrote:
No Such Luck wrote:
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

You are aware that this an unnecessary obfuscation of
"printf("?

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)


This is not valid C.
__VA_ARGS__ is never optional, so just use "..." instead of
"format, ..." and "__VA_ARGS__" instead of "format, ##_VA_ARGS__.
You probably are referring to a compiler extension of a certain
"free" compiler.
Apart from that, fprintf() return EOF on error and usually the
number of characters transmitted.
Minor nit: Keep to the usual convention with upper case macro
names

#include <stdio.h>

#define PRINT_TO(file_ptr, ...) \
do { \
if (fprintf(file_ptr, __VA_ARGS__ ) != 0) \
fflush(file_ptr); \
/* else... handle the error if you like */ \
} while(0)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #5
No Such Luck wrote:
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


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.


Because output is usually _very_ slow compared to the execution
time of the rest of your code. If you switch off buffering
(or are fflush()ing at every output), you usually have to wait
longer as you have additional overhead (initiating output etc.).

Try adding _much_ output to something computationally expensive.
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.


This is OT here, so I will not say much more than "it depends" :-)
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.


Under C99,

#define PRINT(...) \
do { \
printf(__VA_ARGS__); \
fflush(stdout);
} while (0)

(untested)

Under C89, I would either switch off the buffering or write a
wrapper function

int flushprint(const char *format, ...)
{
va_list args;
int ret;

va_start(args, format);
ret = vprintf(format, args);
va_end(args);
if (fflush(stdout))
ret = EOF;

return ret;
}

(untested; in addition to <stdio.h>, you also need <stdarg.h>)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #6
On Mon, 09 May 2005 12:32:25 -0700, No Such Luck wrote:
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


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.


Buffering can greatly improve the performance of the application. As such
you generally want it on much more often than not.
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.


What delay?

Even in situations like this it usually isn't necessary to flush after
every output operation. If for example you have 2 output operations one
after the other in the code you typically don't need to flush after the
first one. Generalising this you only need to flush output when the
program might be blocked or busy doing other things for an extended
period. In that case you can flush output before you read input or do
anything else that might block, or perform a significant calculation. In
many cases you can simplify this to flushing output before reading input,
often at a single point in the code.

Lawrence
Nov 14 '05 #7
>> 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.
What delay?


If you are doing something time-consuming, it is common to output
something to indicate that the program is alive and actually doing
something. For example, it might output "Loading data\n" followed
by the message "Loaded %d records\n" for every thousand records it
loads. There are also programs that output a single period every
so often, again to indicate progress, or output a percentage and/or
estimated time to completion followed by a carriage return (so, on
many systems, it looks like the percentage is being periodically
updated). Or the program might be logging error messages related
to stuff it collects in real time. If you invoke the program
directly, everything works fine. If you invoke the program like
this:
program | tee logfile

then you may not see the first output for a long time or until the
program finishes (which might be "never" for a daemon).
Even in situations like this it usually isn't necessary to flush after
every output operation. If for example you have 2 output operations one
after the other in the code you typically don't need to flush after the
first one. Generalising this you only need to flush output when the
program might be blocked or busy doing other things for an extended
period.
Or when you have produced a complete message that you want to be seen
even if no other messages are generated for a long period of time.
In that case you can flush output before you read input or do
anything else that might block, or perform a significant calculation. In
many cases you can simplify this to flushing output before reading input,
often at a single point in the code.


This is a good point: if you don't flush output before doing input,
you sometimes end up seeing the prompt only AFTER you've answered it.

Gordon L. Burditt
Nov 14 '05 #8
On Tue, 10 May 2005 18:19:40 -0000, go***********@burditt.org (Gordon
Burditt) wrote:
Even in situations like this it usually isn't necessary to flush after
every output operation. If for example you have 2 output operations one
after the other in the code you typically don't need to flush after the
first one. Generalising this you only need to flush output when the
program might be blocked or busy doing other things for an extended
period.


Or when you have produced a complete message that you want to be seen
even if no other messages are generated for a long period of time.


As in writing a trace message when you don't yet know exactly where
the program crashes ;-)

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #9


Gordon Burditt wrote:
[...]
This is a good point: if you don't flush output before doing input,
you sometimes end up seeing the prompt only AFTER you've answered it.


In this situation it's not a "prompt," but a "tardy."

--
Er*********@sun.com

Nov 14 '05 #10
Gordon Burditt wrote:
.... snip ...
This is a good point: if you don't flush output before doing input,
you sometimes end up seeing the prompt only AFTER you've answered it.


My suggestion is simply to automate the flush with a macro:

#define promptf(f, s) do {fputs(s, f); fflush(f); } while (0)

Note that we can't use puts because of the extraneous \n. It might
be better implemented as an inline function when that is available.

inline int promptf(char *s, FILE *f)
{
fputs(s);
return fflush(f);
}

and a define

#define prompt(s) promptf(s, stdout)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #11
On Tue, 10 May 2005 18:19:40 +0000, Gordon Burditt wrote:
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.


What delay?


If you are doing something time-consuming, it is common to output
something to indicate that the program is alive and actually doing
something. For example, it might output "Loading data\n" followed
by the message "Loaded %d records\n" for every thousand records it
loads. There are also programs that output a single period every
so often, again to indicate progress, or output a percentage and/or
estimated time to completion followed by a carriage return (so, on
many systems, it looks like the percentage is being periodically
updated). Or the program might be logging error messages related
to stuff it collects in real time. If you invoke the program
directly, everything works fine. If you invoke the program like
this:
program | tee logfile

then you may not see the first output for a long time or until the
program finishes (which might be "never" for a daemon).


OK, yes, those are the situations we are trying to avoid.
Even in situations like this it usually isn't necessary to flush after
every output operation. If for example you have 2 output operations one
after the other in the code you typically don't need to flush after the
first one. Generalising this you only need to flush output when the
program might be blocked or busy doing other things for an extended
period.


Or when you have produced a complete message that you want to be seen
even if no other messages are generated for a long period of time.


The logic I suggested already covers this case, i.e. if the code performs
operations which may block or otherwise take a long time it first flushes.
This may not be simple or appropriate to implement in all circumstances
but it often is.
In that case you can flush output before you read input or do
anything else that might block, or perform a significant calculation. In
many cases you can simplify this to flushing output before reading input,
often at a single point in the code.


This is a good point: if you don't flush output before doing input,
you sometimes end up seeing the prompt only AFTER you've answered it.


This approach helps buffering by not flushing more than necessary. It
might flush when the output buffer is empty but that's likely to be a
cheap operation. As well as lower call overhead this can improve network
operations by moving data in larger chunks.

Lawrence


Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
3
by: Alexander Ulyanov | last post by:
Hi all. Is it possible to pass the whole blocks of code (possibly including " and ,) as macro parameters? I want to do something like: MACRO(FOO, "Foo", "return "Foobar";", "foo();...
8
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
5
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.