My impression is that this code is used because some compilers won't accept a simpler and more direct approach.
I was wondering if the following would work "cross-platform" (It seems to work ok with the Microsoft Visual Studio vc7.1 compiler, but I am ignorant of other compilers):
#void load_file(std::string& s, std::istream& is) #{ # if(is.bad()) return; # // Use 0 as CrLf delimiter to cause entire file to be read # getline(is, s, '\0'); #}
Looks like a good idea. However one of the main reasons for code being as it is, are rather sub-optimal getline implementations that append one character at a time to the string, without reserving any space first: this results in an O(N^2) algorithm for many string implementations. Previously I used istream iterators and std::copy to copy the stream's contents to the string. It's lovely simple code, and good use of the std lib, but it takes an age to run :-( Basically there are really simple solutions that work well for small files, and then crap out big time as soon as the file size grows. Of course you could always replace std::string with std::vector and get O(N log N) performance, but then you're still open to "why did you do that" questions. John.