Thread: end of file problems

  1. #1
    Registered User GiraffeMan's Avatar
    Join Date
    Feb 2002
    Posts
    20

    Lightbulb end of file problems

    haoy People...
    I've been making this program to identefy a string in a file, and to write down the raws were it appears...
    the problem was that when I wrote just

    while(!feof(in))

    it would read the last raw (string) twice... say I had 6 lines in the text file.. and the string would be in the 6th one..
    I would get - line 6 AND line 7 as an output....

    it had something to do with EOF cause after changing it to that:

    while(fgetc(in)!=EOF)
    {
    fseek(in,-1,SEEK_CUR);
    i++;
    fgets(buf,80,in);
    if((strstr(buf,str))!=NULL) arr[i]=1;
    else arr[i]=0;
    }
    it read it fine....
    can anyone explain why???

    sorry about the long thread...
    THE GIRAFFE MAN
    "our heads in the skies, but our feet on the ground"....

    http://dagan.150m.com

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    !feof() is buggy, use this instead:

    while( fgets( buf, 80, in ) != NULL ) {

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User GiraffeMan's Avatar
    Join Date
    Feb 2002
    Posts
    20

    god bless the ignorent

    Thanks Prelude, I'm still confused though, I know i can use loads of ways to get rid of that, the thing is I want to understand why???
    My teacher (not the brightest bulb in the chandellier) says it's because of the O/S... I say that some how the 32 bit (win98) we are using is seeing EOF as 2 Bytes insted of one...

    HELP...
    O' Wise one....
    THE GIRAFFE MAN
    "our heads in the skies, but our feet on the ground"....

    http://dagan.150m.com

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The reason this happens is because feof() returns the state of the EOF flag. This flag is not set until the EOF has been READ!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    question from intrested party!

    'ello there, listen i'm kind of new and i didn't quite understand something. why does it need to read EOF twice (like you said that EOF needs to be read (that's once) in order to set the flag and then again (that's twice) in order to understand the flag.

    ta muchly!

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Why would you need to read it twice. Once you have read EOF you know the state of the EOF flag so why call feof() and check it again. C'mon use your head!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM