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
A quick note on why rewind failed: it is a C function that works with FILE*, not with C++ stream objects. With std::ifstream, you reposition using stream APIs. is on the right track about moving the read position, and is right that after you hit end-of-file the stream is in an error state and must be reset before you can do anything meaningful with it again.
Before you decide to do a second pass on disk, consider caching. If the file fits in memory, read it once, then process the in-memory data as many times as you like. Two common patterns:
// Whole file as bytes
std::ifstream in("data.bin", std::ios::binary);
std::string bytes((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
// pass 1 on 'bytes' ...
// pass 2 on 'bytes' ... // Line-oriented text
std::ifstream in("data.txt");
std::vector<std::string> lines;
for (std::string line; std::getline(in, line); )
lines.push_back(line);
// iterate 'lines' multiple times If you truly need to re-read from disk, two robust options that build on what and discussed:
// Close, reset state, and reopen
std::ifstream in("data.txt");
// ... read ...
in.close();
in.clear(); // clear eof/fail flags before reuse
in.open("data.txt"); // starts from the beginning // Independent passes without seeking
std::ifstream pass1("data.txt");
std::ifstream pass2("data.txt");
// read with pass1 and pass2 separately Tip: if you care about exact byte positions (e.g., binary parsing), open in std::ios::binary so platform newline translation does not skew offsets and sizes. Also, if closing and reopening did not start at the beginning, it is usually because the stream state was not cleared or the file was opened with a mode like std::ios::ate earlier.
Jump to Post— MosaicFuneral 812What rewind()?
ifstream.seekg(0, ios::beg);
Jump to Post— Freaky_Chris 299The 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 …
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..
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.