Hi,

I wish to read the file twice. I am using ifstream to read the file. I tried using rewind(File pointer). But i get an error. How can i do it.

Thanks

Recommended Answers

What rewind()? ifstream.seekg(0, ios::beg);

Jump to Post

The other option would be to close the file stream and then re-open, btw be sure to check that you actually need to read the file stream twice, rather than loading it contence into some data structure then reading that again. Just pointing that out because file access is slow …

Jump to Post

All 5 Replies

What rewind()? ifstream.seekg(0, ios::beg);

The other option would be to close the file stream and then re-open, btw be sure to check that you actually need to read the file stream twice, rather than loading it contence into some data structure then reading that again. Just pointing that out because file access is slow in comparison.

Chris

The other option would be to close the file stream and then re-open, btw be sure to check that you actually need to read the file stream twice, rather than loading it contence into some data structure then reading that again. Just pointing that out because file access is slow in comparison.

Chris

Closing the file and opening it again in some compilers isnt pointing back to the beginning of the file.

However the second method which Chris said is quiet efficient.

Don't forget to clear file state if eof was reached:

... read file until eof
    // Now f is in fail() and eof(0 state...
    f.clear(); // Now f is capable to seek...
    f.seekg(0);
... read again ...

thanks...seekg works..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.