简体   繁体   English

在C ++中读取文件时跳过EOF

[英]Skip EOF while reading a file in C++

Whenever I encounter a substitute character http://en.wikipedia.org/wiki/Substitute_character while reading a file in C++ using getline(), it is interpreted as a EOF so I cannot advance with my reading in order to get the file entire content. 每当我在使用getline()在C ++中读取文件时遇到替代字符http://en.wikipedia.org/wiki/Substitute_character时,它将被解释为EOF,因此我无法继续阅读以获取完整的文件内容。 So my question is, how can I skip the substitute characters and read the content of the file until the "real" EOF? 所以我的问题是,如何跳过替代字符并读取文件的内容,直到“真正的” EOF?

Open the file in binary mode instead of text mode. 以二进制模式而不是文本模式打开文件。 If you're using fopen , make open it in one of the "b" modes, eg "rb" . 如果您使用的是fopen ,请以"b"模式之一(例如"rb"打开它。 If you're using a C++ ifstream object, open it with the ios::binary flag. 如果您使用的是C ++ ifstream对象,请使用ios::binary标志将其打开。

For example: 例如:

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

// C++ method
std::ifstream f("filename", std::ios::in | std::ios::binary);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM