我试着用这段代码用vc++读取整个文本文件
ifstream file (filePath, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = (long)file.tellg();
char *contents = new char [size];
file.seekg (0, ios::beg);
file.read (contents, size);
file.close();
isInCharString("eat",contents);
delete [] contents;
}
但它不是获取所有整个文件,为什么以及如何处理这个?
注意:文件大小为 1.87 MB 和 39854 行
最佳答案
您缺少以下行
file.seekg (0, file.end);
之前:
size = file.tellg();
file.seekg (0, file.beg);
如本例所述:http://www.cplusplus.com/reference/istream/istream/read/
关于C++ 读取整个文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15547645/