Like Tree2Likes
  • 1 Post By Matticus
  • 1 Post By hk_mp5kpdw

File stream loop

This is a discussion on File stream loop within the C++ Programming forums, part of the General Programming Boards category; Why does my loop loops 3 times when i have only 2 line of data in my file? From the ...

  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 06:25 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    It looks like you have a syntax error, so I doubt your loop loops at all.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    2,273
    Quote Originally Posted by tabstop View Post
    Assuming your while is not b0rken in your actual code...
    Name:  swedish-chef.JPG
Views: 78
Size:  9.9 KB

    Sorry, couldn't resist.
    Elkvis likes this.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    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.
    Salem likes this.
    "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, 12: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, 07: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


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