Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT sources DOT redhat DOT com Delivered-To: mailing list cygwin AT sources DOT redhat DOT com X-Authentication-Warning: hp2.xraylith.wisc.edu: khan owned process doing -bs Date: Thu, 29 Mar 2001 22:31:06 -0600 (CST) From: Mumit Khan To: Uwe Pahner cc: cygwin AT cygwin DOT com Subject: Re: fscanf does not return EOF In-Reply-To: <5.0.0.25.0.20010329081648.00a63b40@barbar.esat.kuleuven.ac.be> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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