fflush()? fscanf() fgets() problem.

Darwin Ling darwinl at alliance.uucp
Wed Jun 5 02:25:58 AEST 1991


In article <456 at equinox.unr.edu> whitbeck at 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
------------------------------------------------------------------




More information about the Comp.lang.c mailing list