Although I found a lot of stuff about std::fstream
on SO, I still can't understand how it operates. Let's see this minimal example:
#include <fstream>
#include <string>
int main()
{
std::fstream fs{ "somefile.txt", std::fstream::out | std::fstream::in };
std::string line;
while (std::getline(fs, line))
{
// reads the whole file
}
// the purpose is to overwrite the whole file
fs.seekp(0, std::ios_base::beg); // moves at the beginning
fs << "Hello world!" << std::endl; // writes in the file
// possibly other read/write
}
This does not work, it seems that one cannot firstly read, and then write in the same stream according to everything I read about. I know the workaround that consists in closing the file, then opening it with the std::ios_base::trunc
flag. However, that seems to be nonsensical: why is there such a limitation? Cannot we technically just overwrite the file after the reading?