2

I have a file which I am reading using ifstream. This file contains 26 numbers separated by whitespace ranging from 0 - 25; "21 1 24 16 10 6 14 3 0 18 20 9 4 19 2 25 7 12 5 11 15 17 8 13 23 22". I am streaming them into my program using the stream::operator >>.

In the code snippet below, I get stuck in an infinite loop. Preceding this code snippet, the following occur:

  1. file is used to read through the file mentioned above, and successfully reaches the end of the file. Strangely, the file is read in in the same manner - using while( !file.eof () ) and storing the read values in an int.
  2. After this, file.clear; and file.seekg(0); are called to clear the EOF flag and move the stream to the start again.
  3. The file ifstream is then passed by reference into a function, where it is also called file. It is in this function where the code snippet below is executed.

    while( !file.eof() ){                                                                                      
        file >> intFromFile;                                                                                                               
        if( counter < 26 ){                                                       
            contacts[counter] = intFromFile; 
            if( repeatValue( contacts, intFromFile, counter )){                     
                return true;                                
            }
        }                                                                       
    
        if( counter == 26 && file.eof() ){         
            file.close();                                                           
            return false;                  
        }
        counter++;
    }
    

Here I end up in an infinite loop, where counter continues to increment, and I never reach the end of the file. I'm unsure how this occurs, if the same framework worked before successfully. The eof flag is never raised. Although, I am not sure why this happens.

If I cout << intFromFile; I get all the numbers in the file printed as expected, "21 1 24 16 10 6 14 3 0 18 20 9 4 19 2 25 7 12 5 11 15 17 8 13 23 22". However, after 22 which is the last number in the file, the loop keeps reading in 0, and never ending. Why is this? The file (to my knowledge) not been changed since the previous read mentioned in 1. .

Any help would be greatly appreciated.

izaak_pyzaak
  • 890
  • 5
  • 21
  • 1
    Does it help to use `while(file) {` instead? – D.R. Nov 13 '15 at 23:14
  • Woah! It worked! Thanks a bunch. :D Why did this work? Is this essentially the implicit reverse if file.eof() ? – izaak_pyzaak Nov 13 '15 at 23:18
  • 5
    [Read this](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). You shouldn't use `eof` at all, and you must always check whether your extract operation succeeded or failed. – M.M Nov 13 '15 at 23:18
  • `file.eof` is true only after an attempt is made to read *beyond* the end of file. Your code assumes that it is true when you've read *up to* and are *at* the end of file. – lurker Nov 13 '15 at 23:19
  • @lurker Sure, I see that the file stream will read in a value. If this value is the end of file flag, then it will only cause `!file.eof()` to evaluate to false upon the next execution of `while( !file.eof())`. I.e it can only signal end of file, once the file stream has passed to the next position, which is undefined. I don't see how this relates to this problem. I'm not trying to read in this undefined value from the file stream, as I should already have stopped reading by then. – izaak_pyzaak Nov 13 '15 at 23:24
  • It is bad style, however, you are right, it should work. If I copy your code to a test project in VS2015 it works just fine (removed repeatValue call) and also it stops without entering an infinite loop. Have you debugged it yet? Maybe `repeatValue` does something funky? What does the debugger say? Which compiler are you using? – D.R. Nov 14 '15 at 00:03

0 Answers0