I want to write to the beginning of a large file using C using fstream
s.
The method I came up with is to write the entire data to a temporary file, then writing to the original file and then copying the data from the tmp file to the original file.
I want to create a buffer which will take the data from the original file to the tmp file and vise-versa.
The process is working for all, string
, ostringstream
and stringstream
. I want the copying of data to happen fast and also with minimum memory consumption.
An EXAMPLE with string
:
void write(std::string& data)
{
std::ifstream fileIN("smth.txt");
std::ofstream fileTMP("smth.txt.tmp");
std::string line = "";
while(getline(fileIN, line))
fileTMP << line << std::endl;
fileIN.close();
fileTMP.close();
std::ifstream file_t_in("smth.txt.tmp"); // file tmp in
std::ofstream fileOUT("smth.txt");
fileOUT << data;
while(getline(file_t_in, line)
fileOUT << line << std::endl;
fileOUT.close();
file_t_in.close();
std::filesystem::remove("smth.txt.tmp");
}
Should I use string
for that or ostringstream
or stringstream
?
What is the advantage of using one over the other?
CodePudding user response:
You don't need to copy manually, there is std::filesystem::copy
.
If you really want to copy using file streams, you can use a one-liner: output_stream << input_stream.rdbuf();
.
And if you really want to copy manually with a loop, it doesn't have to be line-based. Use a fixed-size buffer.
CodePudding user response:
Assuming that there are at least some operation and that you are not copying twice the same data to end with an unchanged file, the possible improvements (IMHO) are:
- do not use
std::endl
inside a loop, but only'\n'
or"\n"
.std::endl
does write an end of line, but also force an flush on the underlying stream which is useless and expensive inside a loop. - you code copies the data twice. It is much more efficient if possible to build a temp file by copying (as your code currently does) and then remove the old file and rename the temp one with the original name. That way you only copy once the data, because renaming a file is a cheap operation.