Thread: Feof replacement

  1. #1
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127

    Feof replacement

    I have been trying to find a good way to replace feof(); but havn't quite found one. I have been trying to read a file line by line until i hit the end of the file and then print if out onto the screen. This is what I have:
    Code:
    while(fgets(temp,sizeof(temp),fp)!=NULL){
                                                         fgets(string,sizeof(string),fp);
                                                         puts(string);
    }
    The problem with this is that it gets the last string twice. How do I fix this?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You're doing fgets(..) twice.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How do I fix this?
    Only call fgets once?
    Code:
    while(fgets(temp,sizeof(temp),fp)!=NULL){
      puts(temp);
    }
    My best code is written with the delete key.

  4. #4
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    The first fgets is into temp while the other is into string. So only one fgets is for string. And only the last line is printed twice.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by 00Sven
    The first fgets is into temp while the other is into string. So only one fgets is for string. And only the last line is printed twice.
    ~Sven
    Clarify please. Are you saying your file has exactly an even number of lines? The first line of each pair (line 1, 3, 5...) gets read into temp and the second line of each pair (line 2, 4, 6...) gets read into string?

    What line number is the last line of the file? To work as described it must be even.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    OK sorry I get it now and it works. Thanks Prelude!
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delayed Replacement Sort
    By Cpro in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2007, 03:36 PM
  2. string padding and replacement functions
    By George2 in forum Tech Board
    Replies: 4
    Last Post: 11-19-2006, 01:40 AM
  3. NeHe Glaux replacement??
    By Razorblade Kiss in forum Game Programming
    Replies: 4
    Last Post: 02-25-2006, 03:30 AM
  4. advice on feof()
    By Hobo in forum C Programming
    Replies: 2
    Last Post: 09-07-2002, 08:15 AM