fread的误区(以文本格式打开文件)

先看一段代码,在读文本文件的时候,第二行可以省略,第四五行一定要有。否则后面代码有坑。(FK微软)
void* buffer = malloc(nLength + 2); //maybe wchar, so + 2
memset(buffer, 0, nLength + 2);
ret = fread(buffer, 1, nLength, m_pFile);
((char*)buffer)[ret] = 0;
((char*)buffer)[ret + 1] = 0;
std::string s = (char*)buffer;


后来干脆这么写
void* buffer = malloc(nLength);
ret = fread(buffer, 1, nLength, m_pFile);
std::string s((const char*)buffer, ret);



===========服了CSDN的代码排版格式了=========

你可能感兴趣的:(C/C++,Win32)