Search code examples
c++stringstringstreamostringstream

Should I use string or ostringstream or stringstream for fileIO in C++


I want to write to the beginning of a large file using C++ using fstreams.
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?


Solution

  • 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.