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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.