Q: Read file to std::string efficiently with windows line endings One of the more efficient ways to read a file into a string in C++ is as follows:
fstream file(filePath);
file.seekg(0, std::ios_base::end);
string fileContents;
fileContents.resize(file.tellg());
file.seekg(0, std::ios_base::beg);
file.read(&fileContents[0], fileContents.size());

If this is on Windows though and the file has Windows style \r\n line endings, then the size of the string will end up larger than its contents due to the fact that tellg() reports the size of the file in bytes, whereas file.read() will translate \r\n into \n resulting in a smaller string than the size of the file.
I can think of several ways of dealing with this, but none of them is efficient nor elegant.
So my first question is: is this actually a problem or have I just made a stupid mistake somewhere in the use of this technique?
My second question then is: what is the most efficient and elegant way of dealing with this?
For what it's worth my current preferred method for dealing with this is to write my text files using Unix line ending style even on Windows, which is proposed in this first link as part of a strategy for C++ coding using utf-8 on Windows, and which the second link seems to hint that Microsoft themselves may be coming around to.
http://utf8everywhere.org/
https://blogs.msdn.microsoft.com/vcblog/2016/02/22/new-options-for-managing-character-sets-in-the-microsoft-cc-compiler/

A: fileContents.resize(file.gcount()) after read() should do the trick.
