lkml.org 
[lkml]   [1997]   [Feb]   [22]   [last100]   RSS Feed
Views: [more markup]  [less markup]  [headers]  [forward] 
 
Messages in this thread
/
DateSat, 22 Feb 1997 10:41:24 -0500 (EST)
FromRussell Berry <>
SubjectRe: Behavior of feof()
Hi Harald, thanks for the response, the rgb.txt I am using is a
modified version of the X11, the first line is gone, and all the
two word color names have been converted to Capitalized one word
names.  I've been writing C code for about 15 years.  When I do
this:

while(result=fscanf(rgb,"%d%d%d%s",&r,&g,&b,cname)!=EOF)
fscanf does indeed return end-of-file when the end is reached,
but many variations of feof() test always fail.  This is a
contradiction to the ANSI C standard and concerns me.  I don't
have a problem getting my program to work, I can write the same
program several different ways in C/C++, asm, java, shell, or
just about any other language available on the Linux system. 
Maybe my initial post was misleading, I don't need help
programming, I am seeking confirmation that the feof() test is
broken, and hopefully and explanation for it.

thanks again...

---russ
On 22-Feb-97 Harald Koenig wrote:
>> I've just encountered a very odd behavior of feof(); on
>> linux-2.0.29.  In the following program:
>
>your use of fscanf() is completely broken!
>have you ever tried to check what is read/written (either check
the output
>or using strace) or even the retiurn value of fscanf() (which
will tell you
>how many items are scanned and written to arguments) ??
>
>>         fscanf(rgb,"%d%d%d%s",&r,&g,&b,cname);
>
>tries to read a decimal number first.
>
>the first lines in my rgb.txt is 
>
>       ! $XConsortium: rgb.txt,v 10.41 94/02/20 18:39:36 rws Exp $
>       255 250 250             snow
>       248 248 255             ghost white
>       248 248 255             GhostWhite
>
>so fscanf will try to convert '!' to a decimal, which fails.
>thus no more scanning is done, fscanf returns 0 (zero) and no
more
>characters are read;  '!' is still the first character.   
>the next call to fscanf will try to convert '!' to a decimal ...
>
>removing that first line and trying again shows that the "snow"
entry
>is correctly scanned and output, but for the 3rd line cname
will get
>"ghost" (which is the next string in the input stream;  NOTE:
strings are
>terminated by white space!!).  now 'w' of "white" is the first
character
>in the input stream.  the next fscanf call will try to convert
'w' to a decimal, 
>which fails...
>
>
>summary:
>a) check return values !!
>b) (almost) never ever use fscanf()  (or be *very* sure about
your format)
>
>a possible solution would be:
>
>       char line[80];
>
>       while (!feof(rgb)) {
>               fgets(rgb, 80, line);
>               if (4 == sscanf(rgb,"%d%d%d%[^\n"]",&r,&g,&b,cname)) {
>                       output new entry ...
>               }
>       }
>
>
>or just
>
>       while (fgets(rgb, 80, line) != EOF) ....
>
>
>
>Harald
>--
>All SCSI disks will from now on                     ___      
_____
>be required to send an email notice                0--,|   
/OOOOOOO\
>24 hours prior to complete hardware failure!      <_/  / 
/OOOOOOOOOOO\
>                                                    \ 
\/OOOOOOOOOOOOOOO\
>                                                      \
OOOOOOOOOOOOOOOOO|//
>Harald Koenig,                                        
\/\/\/\/\/\/\/\/\/
>Inst.f.Theoret.Astrophysik                              //  /  
  \\  \
>koenig@tat.physik.uni-tuebingen.de                     ^^^^^   
   ^^^^^



_______________________________
Berrex Computer Solutions
Turnkey Solutions/Consulting
Software Development
http://www.albany.net/~berrex 
berrex@albany.net
\
 
 \ /
  Last update: 2005-03-22 13:39    [W:0.160 / U:0.030 seconds]
©2003-2008 Jasper Spaans