<cstdio> and fflush(stdout)

Z

zerotypeepytorez

Hi,

I was recently writing a couple of functions to distribute on
comp.sys.sinclair. My code included the use of fflush(stdout) after
some of the output statements (I was using stdio.h instead of iostream
to cater for rather old compilers without an iostream equivalent to
the gets() function, but if anyone answers this I'd like to know if
their answer applies to cstdio), eg:

printf("Are you the Mysterious Murdo?\n");
printf("Enter \'y\' for yes, \'n\' for no.\n");
fflush(stdout);

"You do that in some places but not others." stated one of the group's
posters. I initially took this to be a reference to the fact that I
hadn't flushed the output stream after every printf, but it occurs to
me that this could be saying one or more of:

a.) There are some platforms/situations where you have to include
fflush(stdout) after *every* printf call (as opposed, in my case, to
every group of printfs) else that call's parameter won't be displayed
on the screen.

b.) There are some platforms/situations where one should not use
fflush(stdout).

Can anyone help?

Thanks,

James McLaughlin.
 
A

Ali Cehreli

printf("Are you the Mysterious Murdo?\n");
printf("Enter \'y\' for yes, \'n\' for no.\n");
fflush(stdout);

I googled for 'fflush necessary' and now think that fflush may be
necessary if you didn't pass an '\n' to the output. Some output
streams may not flush the characters unless

a) the buffer is full
b) there is an '\n' sent
c) fflush is called explicitly

According to this, since the output ends with an '\n' above, I don't
think fflush is necessary there. Still, since it's harmless, I think
it would be good practice to keep it in the code in case the previous
printf statement is modified in the future to remove the '\n'.
"You do that in some places but not others." stated one of the group's
posters. I initially took this to be a reference to the fact that I
hadn't flushed the output stream after every printf,

I found that thread. I see that you did not call fflush after every
printf before reading the input. I think that, the person who replied
meant that you forgot to call fflush in some places. Nothing more...
but it occurs to me
that this could be saying one or more of:

a.) There are some platforms/situations where you have to include
fflush(stdout) after *every* printf call (as opposed, in my case, to
every group of printfs) else that call's parameter won't be displayed on
the screen.

This is not true. As you do above, fflush must be called to ensure
that the characters appear at the output. Otherwise the user would not
know what the program expects.

printf(/* ... */);
printf(/* ... */);
printf(/* ... */);
printf(/* ... */);
/* ... */
fflush(stdout);

This is fine and desired because flushing unnecessarily may slow down
the output.

If I am not mistaken, cout and cin of C++ don't have this
problem. Output is flushed automatically before doing input. (Anyone
care to comment on this? Thank you.)
b.) There are some platforms/situations where one should not use
fflush(stdout).

fflush is a standard function. Even if such platforms existed, calling
fflush should be fine.

Ali
 
D

Dave Vandervies

Ali Cehreli said:
If I am not mistaken, cout and cin of C++ don't have this
problem. Output is flushed automatically before doing input. (Anyone
care to comment on this? Thank you.)

I believe that there's a way to request that the runtime library associate
an input and output stream so that the output stream is flushed before
any input operation on the input stream, and that this is defined to
have been done for cin and cout when the program starts (which is then
subject to any changes to this made by the program).


dave
 
A

Ali Cehreli

I believe that there's a way to request that the runtime library
associate an input and output stream so that the output stream is
flushed before any input operation on the input stream, and that this is
defined to have been done for cin and cout when the program starts
(which is then subject to any changes to this made by the program).

Thank you. :) Your response woke me up from my laziness and I opened
Josuttis's The C++ Standard Library. The mechanism is
std::basic_ios::tie. Josuttis says:

<quote>
// predefined connections:
std::cin.tie (&std::cout);
std::wcin.tie (&std::wcout);

This ensures that a message asking for input is flushed before
requesting the input.
</quote>

Ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top