[comp.lang.c] fflush

scs@adam.pika.mit.edu (Steve Summit) (04/08/89)

In article <1383@auspex.auspex.com> guy@auspex.auspex.com (Guy
Harris) correctly points out that:
>..."fflush" doesn't
>flush input, so the ANSI standard says nothing new - it says "it flushes
>output, we make no claims about input", which reflects current practice
>(S5R3's "fflush" appears to flush input, although it's not documented -
>which is kind of obnoxious; either it's a useful feature, in which case
>it should be documented and supported, or it's a useless one, in which
>case it shouldn't have been put in...

(Rahul Dhesi made essentially the same point earlier.)

The trouble with fflush on an input stream is that it isn't
analogous to fflush on an output stream.  fflush on an output
stream means "do the write right now;" however, no equivalent
interpretation is meaningful for an input stream.  Packages that
I know of that do allow fflush on an input stream instead discard
the contents of the buffer, which is very different from what
fflush on an output stream does.

I'm contemplating adding fabort(FILE *) to my stdio library--
this would discard the buffer contents from an input or output
stream, without doing any I/O.  For an input stream, this would
be equivalent to what those extended fflush'es apparently do, but
it avoids the nonportable overloading, while admitting the
possibility of an orthogonal operation on an output stream.  Once
upon a time I wanted such a thing for an output stream, perhaps
for use in a control-C handler.  (I can't remember now; fabort
probably wouldn't have helped anyway.)

Comments?

                                            Steve Summit
                                            scs@adam.pika.mit.edu

whitbeck@sanjuan.wrcr.unr.edu (Mike Whitbeck) (05/31/91)

I have a problem using fscanf() and fgets() and was wondering
if it had something to do with fflush() {I guess I just don't
know what fflush() is for!}

I open a file
	fp = fopen("file","r");

and then I read some stuff
	fscanf(fp,"%f\n",&fv);
then later I try to suck in a line as a text string
	fgets(str,n,fp);
Elsewhere I have used fgets() to read in a line
but here it fails! (gets only the first 'word' (whitespace
delimited) from the line.
As a workaround I use a loop
	for (...) {
		fscanf(fp,"%s",dummy);
		strcat(line," "); 
		strcat(line,dummy);
		}
ICK!

What's going on here? Is fscanf() known to mess up fgets()? or
is this unique to me? [I am using a SUN 3/80].

HELP!

torek@elf.ee.lbl.gov (Chris Torek) (05/31/91)

In article <456@equinox.unr.edu> whitbeck@sanjuan.UUCP (Mike Whitbeck) writes:
>I have a problem using fscanf() and fgets() and was wondering
>if it had something to do with fflush() {I guess I just don't
>know what fflush() is for!}

Fflush() is for output files only (despite anything POSIX says to the
contrary; only output flush is reasonably portable).  It means `take
anything that I asked you to write earlier, and do your darnedest to
see that it gets written'.  Normally, a putchar, printf, fwrite, or
other output operation really means `do this eventually'.  Fflush means
`It is now ``eventually''.'

>I open a file
>	fp = fopen("file","r");
>and then I read some stuff
>	fscanf(fp,"%f\n",&fv);
>then later I try to suck in a line as a text string
>	fgets(str,n,fp);
>Elsewhere I have used fgets() to read in a line
>but here it fails! (gets only the first 'word' (whitespace
>delimited) from the line.

This suggests, but does not prove, that there is a bug in the
implementation you are using.  To prove it you must produce a complete
program (preferably as small as possible), not just code fragments.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov

darwinl@alliance.uucp (Darwin Ling) (06/04/91)

In article <456@equinox.unr.edu> whitbeck@sanjuan.UUCP (Mike Whitbeck) writes:
>I have a problem using fscanf() and fgets() and was wondering
>if it had something to do with fflush() {I guess I just don't
>know what fflush() is for!}
>
>I open a file
>	fp = fopen("file","r");
>
>and then I read some stuff
>	fscanf(fp,"%f\n",&fv);
>then later I try to suck in a line as a text string
>	fgets(str,n,fp);
>Elsewhere I have used fgets() to read in a line
>but here it fails! (gets only the first 'word' (whitespace
>delimited) from the line.
>As a workaround I use a loop
>	for (...) {
>		fscanf(fp,"%s",dummy);
>		strcat(line," "); 
>		strcat(line,dummy);
>		}
>ICK!
>
>What's going on here? Is fscanf() known to mess up fgets()? or
>is this unique to me? [I am using a SUN 3/80].
>
>HELP!

One possible problem is that your buffer's size , namely str's size, may 
not be big enough to store all the characters that you read from the file...

Make sure you have allocated enough memory for str...


------------------------------------------------------------------
				Darwin Ling                       

				Alliance Technologies , Inc
				Advanced Development group
				uunet :     uunet!alliance!darwinl
------------------------------------------------------------------