9

Seekg does not seem to work, when I reach EOF in myFile.

ifstream myFile("/path/file");
for(int i; i < 10; i++){
    myFile.seekg(0);//reset position in myFile
    while(getline(myFile, line)){
        doSomething
    }
}

So, now I am opening input stream every loop:

for(int i; i < 10; i++){
    ifstream myFile("/path/file");//reset position in myFile
    while(getline(myFile, line)){
        doSomething
    }
}

But I would rather seek to position 0. How can I achieve that?

rluks
  • 2,762
  • 8
  • 38
  • 56
  • possible duplicate of [seekg() function fails](http://stackoverflow.com/questions/11264764/seekg-function-fails) – amo Sep 16 '14 at 22:59

1 Answers1

15

Make sure you clear the error flags before the call to myFile.seekg():

myFile.clear();

After the EOF flag has ben set, you will not be able to extract anything. You have to clear those flags to be able to extract again.

Guy L
  • 2,824
  • 2
  • 27
  • 37
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324