Thread: File stream loop

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    File stream loop

    Why does my loop loops 3 times when i have only 2 line of data in my file? From the cout << s.size , when i run it will show 1,2,3.

    Information in txt file
    Code:
    local john 10/2/1990
    international tom 2/5/2000
    output
    Code:
    local john 10/2/1990
    international tom 2/5/2000
    international tom 2/5/2000
    Code:
    ifstream in(filename);
    while()
    {
        if (in.eof())
        break;
        if(in)
        {
            in >> nationality;
            in >> name;
            in >> DOB;
            clsStudent student(nationality, name, DOB);
            s.push_back(student);
        } 
        else
            cerr << "\nCannot open " << filename << " for reading" << endl;
            cout << s.size();
    }
    Last edited by Alexius Lim; 12-04-2013 at 07:25 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you have a syntax error, so I doubt your loop loops at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assuming your while is not b0rken in your actual code, you're using eof to control a loop, which is just being silly. (There's an FAQ about that at this very site, although it might be C-heavy as I recall.)

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by tabstop View Post
    Assuming your while is not b0rken in your actual code...
    File stream loop-swedish-chef-jpg

    Sorry, couldn't resist.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should try looping based on the success of the input(read) operation:
    Code:
    ifstream in(filename);
    while(in >> nationality >> name >> DOB )
    {
        // Do stuff with what you've successfully read
    }
    EOF is not set until AFTER you've attempted to read past the end of the file, not at the point where you've read up to the end of file. The reason you see 3 lines is that after the last line is read the first time, you go back into the while loop. The eof test at this stage will still return false and you will not break out of the loop like you think your program should be doing. Then, your program tries to do the three stream extraction (read) operation statements, these will of course all fail and EOF will be set but the problem is you are already in your processing loop so it is too late to do anything. Since the read operation has failed, the values in the variables from the previous (successful) read operation are still present and you print out the contents one more time.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using read on tcp socket stream unable to exit loop.
    By dtow1 in forum Linux Programming
    Replies: 4
    Last Post: 03-04-2013, 01:57 PM
  2. fwrite function to write png file stream into a file
    By mdivya in forum C Programming
    Replies: 2
    Last Post: 08-26-2011, 12:10 AM
  3. linking file stream with input file?
    By flamehead144 in forum C Programming
    Replies: 8
    Last Post: 02-07-2009, 08:55 PM
  4. File Stream I/O
    By pldd4 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2002, 08:15 AM
  5. Stream vs. FILE*
    By Aran in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2001, 02:54 AM

Tags for this Thread