[英]ifstream::read fails at ASCII 26
This problem has been bugging me for a long time. 这个问题长期困扰着我。 For example, the code
例如,代码
ifstream in;
char temp;
int a;
in.open ("Random.txt");
for (a = 0;a < 10000;a++)
in.read (&temp, 1);
in.close ();
works fine until the ifstream
encounters a substitute character (ASCII = 26). 工作正常,直到
ifstream
遇到替换字符(ASCII = 26)。 Then, for all following characters, ifstream::read
gives me temp = -1
. 然后,对于所有后续字符,
ifstream::read
给我temp = -1
。 I don't really want this to happen, but instead want it to keep on reading characters from the file instead of -1
. 我真的不希望这种情况发生,而是希望它继续从文件中读取字符而不是
-1
。 What have I done wrong? 我做错了什么?
You need to open the stream in binary mode. 您需要以二进制模式打开流。 For historic reasons the text mode on Windows will consider Control-Z (ASCII 26) as the end of a file.
由于历史原因,Windows上的文本模式会将Control-Z(ASCII 26)视为文件的结尾。
There are more details in this earlier answer of mine . 我之前的回答中有更多细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.