Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, November 24, 2016

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

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


How to 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()'ed data(). This requires the std::string's data to be contiguous which is not required by the standard but 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.

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

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

taking into account the discussion above.

Answer by Jason Cohen for What is the best way to read an entire file into a std::string in c++?


Note that you still have some things underspecified. For example, what's the character encoding of the file? Will you attempt to auto-detect (which works only in a few specific cases)? Will you honor e.g. XML headers telling you the encoding of the file?

Also there's no such thing as "text mode" or "binary mode" -- are you thinking FTP?

Answer by Ben Collins for What is the best way to read an entire file into a std::string in c++?


#include   #include   #include     int main()  {    std::ifstream input("file.txt");    std::stringstream sstr;      while(input >> sstr.rdbuf());      std::cout << sstr.str() << std::endl;  }  

or something very close. I don't have a stdlib reference open to double-check myself.

edit: updated for syntax errors. whoops.

edit: yes, I understand I didn't write the slurp function as asked.

Answer by Thorsten79 for What is the best way to read an entire file into a std::string in c++?


Never write into the std::string's const char * buffer. Never ever! Doing so is a massive mistake.

Reserve() space for the whole string in your std::string, read chunks from your file of reasonable size into a buffer and append() it. How large the chunks have to be depends on your input file size. I'm pretty sure all other portable and STL-compliant mechanisms will do the same (yet may look prettier)

Answer by Konrad Rudolph for What is the best way to read an entire file into a std::string in c++?


The shortest variant: Live On Coliru

std::string str(std::istreambuf_iterator{ifs}, {});  

It requires the header .

There were some reports that this method is slower than preallocating the string and using std::istream::read. However, on a modern compiler with optimisations enabled this no longer seems to be the case, though the relative performance of various methods seems to be highly compiler dependent.

Answer by Matt Price for What is the best way to read an entire file into a std::string in c++?


Something like this shouldn't be too bad:

void slurp(std::string& data, const std::string& filename, bool is_binary)  {      std::ios_base::openmode openmode = ios::ate | ios::in;      if (is_binary)          openmode |= ios::binary;      ifstream file(filename.c_str(), openmode);      data.clear();      data.reserve(file.tellg());      file.seekg(0, ios::beg);      data.append(istreambuf_iterator(file.rdbuf()),                   istreambuf_iterator());  }  

The advantage here is that we do the reserve first so we won't have to grow the string as we read things in. The disadvantage is that we do it char by char. A smarter version could grab the whole read buf and then call underflow.

Answer by Martin Cote for What is the best way to read an entire file into a std::string in c++?


You can use the 'std::getline' function, and specify 'eof' as the delimiter. The resulting code is a little bit obscure though:

std::string data;  std::ifstream in( "test.txt" );  std::getline( in, data, std::string::traits_type::to_char_type(                     std::string::traits_type::eof() ) );  

Answer by Konrad Rudolph for What is the best way to read an entire file into a std::string in c++?


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

string str(static_cast(stringstream() << in.rdbuf()).str());  

This requires the additional header 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:

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

Or, once again in a single line:

string slurp(ifstream& in) {      return static_cast(stringstream() << in.rdbuf()).str();  }  

Answer by paxos1977 for What is the best way to read an entire file into a std::string in c++?


See this answer on a similar question.

For your convenience, I'm reposting CTT's solution:

string readFile2(const string &fileName)  {      ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);        ifstream::pos_type fileSize = ifs.tellg();      ifs.seekg(0, ios::beg);        vector bytes(fileSize);      ifs.read(&bytes[0], fileSize);        return string(&bytes[0], fileSize);  }  

This solution resulted in about 20% faster execution times than the other answers presented here, when taking the average of 100 runs against the text of Moby Dick (1.3M). Not bad for a portable C++ solution, I would like to see the results of mmap'ing the file ;)

Answer by Gavriel Feria for What is the best way to read an entire file into a std::string in c++?


What if you are slurping a 11K file, then you have to do it in a series of chunks, so you have to use something like std::vector to slurp it in large chunks of strings.

Answer by wdavilaneto for What is the best way to read an entire file into a std::string in c++?


Well, if you are looking for a simple and 'readable' way to do it. I would recomend add/use some high level framework on your project. For that I's always use Poco on all my projects. In this case, with Poco:

string text;  FileStream fstream(TEXT_FILE_PATH);  StreamCopier::copyToString(fstream, text);  

Best Regards


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.