What is the best way to read an entire file into a std::string in C++?


How do I read a file into a std::string, i.e., read the whole file at once?

Text or binary mode should be specified by the caller. The solution should be standard-compliant, portable and efficient. It should not needlessly copy the string's data, and it should avoid reallocations of memory while reading the string.

One way to do this would be to stat the filesize, resize the std::string and fread() into the std::string's const_cast<char*>()'ed data(). This requires the std::string's data to be contiguous which is not required by the standard, but it appears to be the case for all known implementations. What is worse, if the file is read in text mode, the std::string's size may not equal the file's size.

A fully correct, standard-compliant and portable solutions could be constructed using std::ifstream's rdbuf() into a std::ostringstream and from there into a std::string. However, this could copy the string data and/or needlessly reallocate memory. Are all relevant standard library implementations smart enough to avoid all unnecessary overhead? Is there another way to do it? Did I miss some hidden Boost function that already provides the desired functionality?

Please show your suggestion how to implement it.

void slurp(std::string& data, bool is_binary)

taking into account the discussion above.

3/4/2017 5:12:07 AM

And the fastest (that I know of, discounting memory-mapped files):

std::string str(static_cast<std::stringstream const&>(std::stringstream() << in.rdbuf()).str());

This requires the additional header <sstream> for the string stream. (The static_cast is necessary since operator << returns a plain old ostream& but we know that in reality it’s a stringstream& so the cast is safe.)

Split into multiple lines, moving the temporary into a variable, we get a more readable code:

std::string slurp(std::ifstream& in) {
    std::stringstream sstr;
    sstr << in.rdbuf();
    return sstr.str();

Or, once again in a single line:

std::string slurp(std::ifstream& in) {
    return static_cast<std::stringstream const&>(std::stringstream() << in.rdbuf()).str();
5/29/2018 8:31:49 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow