Why does seekg not work with getline?

Seekg doesn't seem to work when I achieve 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 open the input stream in each loop:

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

But I would rather like to position 0. How can I achieve this?

+5
source share
1 answer

Before calling, myFile.seekg()make sure you clear the error flags:

myFile.clear();

Once the EOF flag is set to ben, you cannot extract anything. You must clear these flags in order to extract them again.

+10
source

All Articles