Thread: eof

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    32

    eof

    Hi everyone

    I did a counter to get the value from a simple number file, because I used the counter to
    Code:
    while ( counter <=10)
    {
    value=getline( filename, line);
    sum+=value;
    counter++;
    }
    now the problem if I had less than 10 lins in that file. it will give result of ( abnormal termination).

    so, I changed it with
    Code:
    ifstream indata(filename)
    while ( !filename.eof)
    {
    value=getline( filename, line);
    sum+=value;
    counter++;
    }
    but this gives me as well termination error with my orignal file with 10 lines and same error with a file of less than 10 line.
    I know eof is not a good thing to terminate and exit the loop. but what else could I use.

    so anyidea with example to fix similar error.

    thank you all

    all the result are wrong

  2. #2
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Question try this...

    I'm not sure, but try this.
    Code:
    do{
        value = getline(filestreamobject, line);
        if(!filestreamobject)
            break;
        sum += value;
    counter++;
    }while(counter<10);
    P.S.
    Why are you using getline(a_string, int);
    should'nt it be getline(streamobject, int); ???
    Last edited by arjunajay; 02-02-2006 at 05:02 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I know eof is not a good thing to terminate and exit the loop. but what else could I use.
    Standard practice is to use your read statement as a while conditional:
    Code:
    ifstream indata(filename);
    string line;
    
    while( getline(indata, line) )
    {
    
    }
    getline() returns the stream object indata, which will evaluate to true or false when it is in a while conditional(i.e. in a boolean context). If any stream error occurs, indata will evaluate to false, and the while loop will end. EOF is considered a stream error. So, when you reach the end of the file and there is no more data, the while loop will end. There are also other stream errors that can occur, and they will also cause the while loop to end, preventing an infinite loop which will occur if you use eof() as your while conditional.
    Last edited by 7stud; 02-02-2006 at 05:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM