detecting end of file

This is a discussion on detecting end of file within the C++ Programming forums, part of the General Programming Boards category; Trying to do something like... Code: while(!fin.eof()) { // stuff here } But apparently fin.eof() doesn't work...what can I check ...

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    detecting end of file

    Trying to do something like...

    Code:
    while(!fin.eof())
    {
         // stuff here
    }
    But apparently fin.eof() doesn't work...what can I check for?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Using eof() as the loop condition is a Bad Thing, the eofbit is only set after you've tried to read and failed. The end result is usually that you read one more than you want. Try placing your input in the condition of the loop, for example:
    Code:
    while ( cin>> var ) {
      // All is well, work with var
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Thanks, got it to work!
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    suppose you had
    char ch;
    fin.get(ch);
    how does one check for eof of ch?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. checking for end of file
    By linucksrox in forum C Programming
    Replies: 7
    Last Post: 06-01-2004, 01:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21