This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: fscanf does not return EOF


On Thu, 29 Mar 2001, Uwe Pahner wrote:

> Hi,
> 
> I am new to Cygwin and I am trying to port a code that is succesfully 
> running with the HP-UX-C-compiler and the Linux-gcc.
> I installed cygwin two days ago, and found it very helpful so far. I try to 
> use the gcc version gcc-2.95.2-9 19991024 (cygwin experimental)
> to compile my code.
> 
> It seems that fscanf() used on a text-file-pointer does not return an EOF when
> reaching the end of the file. Instead fscanf returns 0. This is not very 
> helpful, as an empty line would return the same value. The effect to my 
> program is that it ends up looping endlessly in while().
> 
> Here a part of the code that presents the problem. I attempt to read the 
> text file line by line in the while loop, and to stop the reading at EOF. 
> My HP and Linux code has the fscanf format instruction slightly different: 
> " %[^\n]".
> 
> char linetxt[BUFSIZ];
> infile = fopen(polyfilename, "r");
> if (infile == (FILE *) NULL) {
> {
> ....
> }
> 
> while (fscanf(infile," %[^\r]",linetxt)!=EOF)
> {
> ...
> }
> 
> Has anyone seen this behaviour, or am I doing something wrong?

I believe the specifications for *scanf routines are rather confusing
when it comes returning EOF, and I believe Cygwin/newlib is doing the 
right thing. The condition for *scanf routines returning EOF are the
following:
  
1. if the input ends before the first matching failure or conversion, or
2. read error on the stream (or string for sscanf)

As an example, the following will *not* return EOF:
  
  sscanf("foo", %d, &var);

but this will:

  sscanf("", %d, &var);   /* condition 1 */

I suggest portable coding, where you read the lines one by one using
fgets, checking for EOF, and if not, pass the line buffer it to sscanf.
Or, use fscanf, but check explicitly for eof on the stream when you
see a return value of 0.

I may of course be reading the specification wrong.

Regards,
Mumit



--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]