使用C++读取文件时跳过EOF

Dav*_*d A 4 c++ file-io getline eof

每当我使用getline()在C++中读取文件时遇到替换字符http://en.wikipedia.org/wiki/Substitute_character时,它会被解释为EOF,所以我无法提前阅读以获得整个文件内容.所以我的问题是,如何跳过替换字符并读取文件的内容,直到"真正的"EOF?

Ada*_*eld 6

以二进制模式而不是文本模式打开文件.如果您正在使用fopen,请在其中一种"b"模式下打开它,例如"rb".如果您正在使用C++ ifstream对象,请使用ios::binary标志打开它.

例如:

// C method
FILE *f = fopen("filename", "rb");

// C++ method
std::ifstream f("filename", std::ios::in | std::ios::binary);
Run Code Online (Sandbox Code Playgroud)

  • 由于显而易见的原因,在`ifstream`中不需要`std :: ios :: in` ... (2认同)