[g++] re-using an ifstream

Kinniken

Registered
Hi, I need to read a file twice in one of my algorithm. My code, simplified, looks like that:

PHP:
ifstream ifile;
ifile.open(fname.c_str(), ios::in);

//lots of code reading data using .get()

ifile.close();
ifile.open(fname.c_str(), ios::in);

//an other batch of code using .get()

Now, what I was expecting was for the first part to execute on the file (it does), and then for the second part to do the same, re-starting from the beginning. It does not, instead behaving as if the file was empty.
I also tried using .seekg() to manually move back the pointer, it failed as well. Of course, I can always declare a second stream, but it's stupid.
Anyone has any idea of what I am doing wrong?

TIA,

Kinniken
 
I'm not sure, but when you open the file again, instead of "ios::in", try "ios_base::beg", which tells it to move to the beginning of the stream. For more details though, just type in "fstream" at cplusplus.com, a wonderful wonderful reference site, though a lot of it is actually in c, not c++. I hope that helps you though?
 
I just cut and pasted, but replace, "ios_base::beg" with just "ios::beg"
here it is corrected:
(ios_base::beg != ios::beg)

Apologies, and I hope it works.
 
Back
Top