我一直在努力解决这个问题一周,并且离解决方案还很远。
我正在通过字符串从文件中读取纯文本。它成功地读取所有内容,直到具有特定大数字的字符。 此编号特定于每个文件,并且对于不同的文件是不同的。
到达具有此大数字的字符后,它会读取“\ x01 \ 0 \ 0 \ 0”序列(由4个字符组成)一次或多次而不是原始字符,然后正确读取所有内容(直到下一个大数字。)
用几句话来说 - 而不是读这个:
... many characters ...
First read failure!
... many characters ...
Second read failure, second read failure!
... many characters ...
etc.
它读到了这个:
... many characters ...
First read f\x01\0\0\0re
... many characters ...
Second read failu\x01\0\0\0\x01\0\0\0\x01\0\0\0ead failure!
... many characters ...
etc.
您对此问题的原因有什么想法吗?
其他信息:
1) The "ferror" condition is not true.
2) I am reading from file using streams (fopen, fread, fclose)
3) Have tested different read methods: "fread cycle" and "fgets".
The results are the same.
4) The binary optimization is disabled in the compiler's settings.
It seams that the problem is not connected with a compiler.
Neither GCC, nor Apple LLVM gives me the desired result.
5) Attempting to solve the issue, I converted the whole project from C++ to C,
but the problem doesn't disappear.
答案 0 :(得分:1)
fread()
不一定是NUL终止输入。如果要将输入视为字符串
chk = fread(input, <WHATEVER>);
if (chk > 0) {
input[chk] = 0; /* terminate input */
/* WHATEVER */
}
答案 1 :(得分:1)