fopen()std::string.线应仅由\n其他变体分隔,而不是其他变体.string ReadWhole()
{
Seek(0);
char *data = new char[GetSize()];
if (1 != fread(data, GetSize(), 1, mFile))
FATAL(Text::Format("Read error: {0}", strerror(errno)));
string ret(data, GetSize());
delete[] data;
return ret;
}
Run Code Online (Sandbox Code Playgroud)
作为参考,这是GetSize,但它只返回文件的大小(缓存):
int GetSize()
{
if (mFileSize)
return mFileSize;
const int current_position = ftell(mFile);
fseek(mFile, 0, SEEK_END);
mFileSize = ftell(mFile);
fseek(mFile, current_position, SEEK_SET);
return mFileSize;
}
Run Code Online (Sandbox Code Playgroud)
fread()失败,因为文件有\r\n行结尾,它们只计为1个字符而不是2个字符,所以它尝试读取的文件超过了文件中的字符.
我可以解决它,fgets但我想知道是否有更好的方法.谢谢.