Possible Duplicate:
What is the best way to slurp a file into a std::string in c++?
I'm trying to imitate PHP's file_get_contents() function for C++.
However, when I convert a char array into a string, it stops at nullbyte:
fread(charbuf, 1, file_size, fp);
string str(charbuf);
How can I initialize the string as a static size array, and read the file contents directly to that container? Also, how do I check the errors for it, for example if there is not enough memory for initializing that string. This would also get me rid of the temporary memory allocation I'm currently using, which I would like to get rid of.
How about safety? Is it possible that many processes read the same file at same time and/or one of them writes in it at same time when I am reading it? How do I avoid such things happening?
I hope you can answer other way than "string isn't binary container".
I ask to reopen this question for the fact: "Apparently, this question is as relevant as ever: two years later, the two most efficient solutions still copy the whole file contents in memory, and this copy cannot be elided by the optimizer. This is a quite unsatisfactory state of affairs. – Konrad Rudolph Oct 25 '10 at 6:25" at What is the best way to read an entire file into a std::string in C++? Or do you want me to create new question that asks to read a file without having extra copy of the string?