Need help : eof, loop, output file

This is a discussion on Need help : eof, loop, output file within the C++ Programming forums, part of the General Programming Boards category; Code: while (!inFileEvening.eof() ) { // store data from file to variables inFileEvening >> studenteveningId >> firstname >> surname >> ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    6

    Need help : eof, loop, output file

    Code:
       while (!inFileEvening.eof() )
       { 
          // store data from file to variables
          inFileEvening >> studenteveningId >> firstname >> surname >> num1 >> num2 >> num3 >> num4 >> num5;
          
    
          // formatting for name    
          studentname = surname + ", " + firstname;
          while(studentname.length() != 20)
          {
             studentname = studentname + " ";
          }
    Im using that, i have that loop twice in my code, and when it outputs to reports_regular.txt , it displays the last student twice for some reason, so if my studentsDay.txt and studentsEvening.txt have 30 students listed altogether, the number of students shown will be 32, because the last person is added again on each output files.

    I've read over the forums about while (!inFileEvening.eof() ) not working properly with reading lines?? i have no idea, please help
    thanks

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Well, yes, you should rather use something like:
    Code:
    while(inFileEvening>>studenteveningId)
    {
    inFileEvening>>firstname>>...
    ...
    The problem with while(!File.eof()) is that if you have, say, a couple of blank lines at the end of the file, EOF will not be reported when those are reached and the loop will continue though the lines are empty. That is, your program will try to read values from the file which are not there. This will make values of your variables corrupted and your program may crash.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    You have some whitespace at the end of your file e.g a space, tab, or newline. The operator>> is defined to skip leading whitespace, start reading in data, and stop reading when it encounters whitespace, and very importantly it leaves the trailing whitespace in the stream. If there is some whitespace at the end of your file, then the operator>> reads the last piece of data, and stops at the whitespace so it doesn't hit eof because that whitespace character is left in the stream. Then, you output the last piece of data, but since you haven't hit eof, your loop continues. For the next read, the operator>> skips the whitespace remaining in the stream and hits eof without finding any new data. As a result, the old data in your variables remains unchanged, and it is output again. So, any whitespace at the end of your file is causing a duplicate line.

    Also, your while condition could cause an infinite loop. Other stream errors can occur besides eof, and if they do you won't be able to read from the stream, but you won't have hit eof either, with the result being an infinite loop. For instance, a non eof stream error would occur if you tried to read some characters into a numeric variable. The solution is to use your read statement as your while loop condition:


    while(inFileEvening >> studenteveningId >> firstname >> surname >> num1 >> num2 >> num3 >> num4 >> num5)

    The read statement returns a reference to your stream, and when the stream is used in a boolean context, it is converted to true or false. It will be converted to false if there are other stream errors. Even though that while condition cannot detect eof, it will work correctly because when you try to read data when there is no more data left, a stream error will occur that serves to terminate the while loop. Since you are reading in the data and deciding whether to continue the while loop before using the data, you won't get the same duplicate line.
    Last edited by 7stud; 05-05-2005 at 07:32 AM.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    6
    thanks guys
    ok , i've replaced the eof code, with the white( inFile etc etc.)
    but now the loop aint working properly, it is skipping the first student, then the 3rd, then 5th, so its leaving out every second student including the first student... do you know how i can fix it?

    Code:
    +++++Section 1 Student List+++++
    
    s1236  Bridges, Tony       D  82.20  D
    s3232  Smith, Ellisa       D  83.40  D
    s3343  Johnson, Samuel     D  68.60  Cr
    s3434  Jones, Christina    D  59.60  P
    s3444  Nielson, Anna       D  80.20  D
    s4343  Sloane, Jack        D  89.40  HD
    that is what appears..

    and this is my code..

    Code:
     while(inFileDay >> studentdayId >> firstname >> surname >> num1 >> num2 >> num3 >> num4 >> num5)
       { 
    stuff
          {
             stuff
          }
          
          stuff
          {
             stuff
          }
    
          
           {     
           stuff  
           }
    
    stuff
        }
     while(inFileEvening >> studenteveningId >> firstname >> surname >> num1 >> num2 >> num3 >> num4 >> num5)
       { 
    stuff
          {
             stuff
          }
          
          stuff
          {
             stuff
          }
    
          
           {     
           stuff  
           }
    
    stuff
        }

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    6
    hey guys, sori i figured it out, haha silly me,
    well yeah, thanks 7stud and Mathfan, you guys were a great help
    much appreciated!

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It sounds like you are doing this:
    Code:
    while (inFile>>studenteveningId>>..... )
    { 
    	
    	inFileEvening >> studenteveningId >> ....;
    }
    What you needed to do was move the existing read inside your while loop up into the while condition--not add an additional read to the while condition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching Binary Files for a Pattern
    By CaptainMorgan in forum C Programming
    Replies: 16
    Last Post: 06-17-2007, 07:04 PM
  2. EOF (End Of File) Loop help
    By boxsterh in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2006, 03:10 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 06:28 AM

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