Original Post
I have to read a Pascal-string (sequence of characters preceded by their number) from a file to a string. What I do: This way I do an extra copy of all the characters (it may be even some megabytes). I can for sure live with that, but the faster, the better. And the cleaner, the better. So I tried to read the data directly into the string:
std::string ReadPascalString(const std::string& filename) {
std::ifstream file(filename.c_str(), std::ios_base::in | std::ios_base::binary);
int size;
file >> size;
boost::scoped_array<char> buffer(new char[size]);
file.read(buffer.get(), size);
return std::string(buffer.get(), size);
}
std::string result(size);
file >> std::setw(size) >> result;...but this stops at the first space found (at most size chars are read). Any suggestion is appreciated!