http://insanecoding.blogspot.co.uk/2011/11/how-to-read-in-file-in-c.html reviews a number of ways of reading an entire file into a string in C++. The key code for the fastest option looks like this:
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
Unfortunately, this is not safe as it relies on the string
being implemented in a particular way. If, for example, the implementation was sharing strings then modifying the data at &contents[0]
could affect strings other than the one being read. (More generally, there's no guarantee that this won't trash arbitrary memory -- it's unlikely to happen in practice, but it's not good practice to rely on that.)
C++ and the STL are designed to provide features that are efficient as C, so one would expect there to be a version of the above that was just as fast but guaranteed to be safe.
In the case of vector<T>
, there are functions which can be used to access the raw data, which can be used to read a vector efficiently:
T* vector::data();
const T* vector::data() const;
The first of these can be used to read a vector<T>
efficiently. Unfortunately, the string
equivalent only provides the const
variant:
const char* string::data() const noexcept;
So this cannot be used to read a string efficiently. (Presumably the non-const
variant is omitted to support the shared string implementation.)
I have also checked the string constructors, but the ones that accept a char*
copy the data -- there's no option to move it.
Is there a safe and fast way of reading the whole contents of a file into a string?
It may be worth noting that I want to read a string
rather than a vector<char>
so that I can access the resulting data using a istringstream
. There's no equivalent of that for vector<char>
.