I Need to read a file line-by-line twice. The file content is expected to fit into memory. So, I would normally read the whole file into a buffer and work with that buffer afterwards.
However, since I would like to use std::getline
, I need to work with a std::basic_istream
. So, I thought it would be a good idea to write
std::ifstream file(filepath);
std::stringstream ss;
ss << file.rdbuf();
for (std::string line; std::getline(ss, line);)
{
}
However, I'm not sure what exactly is happening here. I guess ss << file.rdbuf();
does not read the file into any internal buffer of ss
. Actual file access should occure only at std::getline(ss, line);
.
So, with a second for-loop of the provided form, I should end in reading the whole file once again. That's inefficient.
Am I correct and hence need to come up with an other approach?