[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

fflush

Bill Cunningham

9/1/2011 7:58:00 PM

Where is the right place to put fflush in a file IO operation? Right
before fclose? I never have really used it to flush buffers only to flush
stdout. Is it a good idea to use fflush all the time?

Bill


26 Answers

Willem

9/1/2011 8:09:00 PM

0

Bill Cunningham wrote:
) Where is the right place to put fflush in a file IO operation? Right
) before fclose? I never have really used it to flush buffers only to flush
) stdout. Is it a good idea to use fflush all the time?

Simple answer: Don't use fflush.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

John Gordon

9/1/2011 8:24:00 PM

0

In <4e5fe3b1$0$27973$bbae4d71@news.suddenlink.net> "Bill Cunningham" <nospam@nspam.invalid> writes:

> Where is the right place to put fflush in a file IO operation? Right
> before fclose? I never have really used it to flush buffers only to flush
> stdout. Is it a good idea to use fflush all the time?

Closing the file automatically flushes the output, so calling fflush right
before closing the file won't accomplish anything.

Fflush is typically used when you want to ensure that some output gets
written to the file RIGHT NOW but aren't ready to close the file yet.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

unknown

9/1/2011 8:49:00 PM

0

John Gordon writes:
> In <4e5fe3b1$0$27973$bbae4d71@news.suddenlink.net> "Bill Cunningham"
> <nospam@nspam.invalid> writes:
>
>> Where is the right place to put fflush in a file IO operation?
>> Right
>> before fclose? I never have really used it to flush buffers only to
>> flush stdout. Is it a good idea to use fflush all the time?
>
> Closing the file automatically flushes the output, so calling fflush
> right before closing the file won't accomplish anything.
>
> Fflush is typically used when you want to ensure that some output gets
> written to the file RIGHT NOW but aren't ready to close the file yet.

In my experience, fflush() is most typically used on input streams,
however this is technically an undefined behavior.

Joe Pfeiffer

9/1/2011 8:55:00 PM

0

"Bill Cunningham" <nospam@nspam.invalid> writes:

> Where is the right place to put fflush in a file IO operation? Right
> before fclose? I never have really used it to flush buffers only to flush
> stdout. Is it a good idea to use fflush all the time?

You only need to use it if you want to force IO to happen immediately;
there aren't many cases where that's necessary.

One place you *don't* need it is just before an fcloew(), as fclose()
does a flush.
--
"Erwin, have you seen the cat?" -- Mrs. Shroedinger

Kleuskes & Moos

9/1/2011 10:07:00 PM

0

On Thu, 01 Sep 2011 20:23:50 +0000, John Gordon wrote:

> In <4e5fe3b1$0$27973$bbae4d71@news.suddenlink.net> "Bill Cunningham"
> <nospam@nspam.invalid> writes:
>
>> Where is the right place to put fflush in a file IO operation?
>> Right
>> before fclose? I never have really used it to flush buffers only to
>> flush stdout. Is it a good idea to use fflush all the time?
>
> Closing the file automatically flushes the output, so calling fflush
> right before closing the file won't accomplish anything.
>
> Fflush is typically used when you want to ensure that some output gets
> written to the file RIGHT NOW but aren't ready to close the file yet.

There is a line of thought that says that under those circumstances you
might want to consider using unbuffered I/O instead.

-------------------------------------------------------------------------------
_______________________________________
/ Yow! Maybe I should have asked for my \
\ Neutron Bomb in PAISLEY -- /
---------------------------------------
\
\
___
{~._.~}
( Y )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------

pete

9/1/2011 11:15:00 PM

0

Bill Cunningham wrote:
>
> Where is the right place to put fflush in a file IO operation?

If you want a message to appear on standard output,
which asks the user for input,
then use fflush(stdout) after the message code,
before the input code.

/* BEGIN new.c */

#include <stdio.h>

int
main(void)
{
int integer;

printf("Enter an integer: ");
fflush(stdout);
if (scanf("%d", &integer) == 1) {
printf("\nThe integer is %d\n", integer);
} else {
puts("\nTry harder next time!");
}
return 0;
}

/* END new.c */


--
pete

Joe Pfeiffer

9/1/2011 11:27:00 PM

0

pete <pfiland@mindspring.com> writes:

> Bill Cunningham wrote:
>>
>> Where is the right place to put fflush in a file IO operation?
>
> If you want a message to appear on standard output,
> which asks the user for input,
> then use fflush(stdout) after the message code,
> before the input code.

<snip code example>

At least under Linux, the attempt to read the input causes the output to
be flushed (and I verified that this worked for your code example with
the fflush() snipped). Is this not portable behavior?

pete

9/1/2011 11:33:00 PM

0

Joe Pfeiffer wrote:
>
> pete <pfiland@mindspring.com> writes:
>
> > Bill Cunningham wrote:
> >>
> >> Where is the right place to put fflush in a file IO operation?
> >
> > If you want a message to appear on standard output,
> > which asks the user for input,
> > then use fflush(stdout) after the message code,
> > before the input code.
>
> <snip code example>
>
> At least under Linux,
> the attempt to read the input causes the output to
> be flushed (and I verified that this worked for your code example with
> the fflush() snipped). Is this not portable behavior?

Portability isn't a boolean quantity.

That behavior is not guaranteed though.

--
pete

John Doe

9/1/2011 11:35:00 PM

0

On Thu, 01 Sep 2011 15:57:37 -0400, Bill Cunningham wrote:

> Where is the right place to put fflush in a file IO operation?

There isn't a "right place". If fflush() is needed at all, the reason
/why/ it's needed dictates /where/ it's needed.

> Right before fclose?

No. That's one place where fflush() is never required, as fclose() will
flush the stream.

I never have really used it to flush buffers only to flush
> stdout. Is it a good idea to use fflush all the time?

No. Excessive use of fflush() may impose a performance penalty.

Seebs

9/2/2011 1:36:00 AM

0

On 2011-09-01, Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
> At least under Linux, the attempt to read the input causes the output to
> be flushed (and I verified that this worked for your code example with
> the fflush() snipped). Is this not portable behavior?

Sort of!

It's recommended, but not strictly required, by C99. It seems to be common
on modernish Unixes, but it was pretty uncommon at some point in the past,
which made it become a FAQ that you needed an fflush() to force the prompt
to display.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.