【发布时间】:2012-03-22 04:14:56
【问题描述】:
我已经为这个问题苦苦挣扎了一个星期,距离解决方案还很远。
我正在通过字符串从文件中读取纯文本。它成功读取所有内容,直到一个字符具有一定的大数字。 这个数字对于每个文件是特定的,并且对于不同的文件是不同的。
到达这个大数字的字符后,它会读取“\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.
【问题讨论】:
-
这可能是硬件故障,或者程序中的错误(可能是一些缓冲区溢出)。您确定磁盘状况良好吗? (最近是否通过
fsck或类似方法检查过文件系统?)您是否通过编辑器检查您的文本文件是否包含您认为的内容?文本文件是如何产生的?或者该文件的生产者可能有问题。或者可能是系统配置错误问题(字符编码......)。您是否使用valgrind运行您的程序? -
如果您在这里需要帮助,您真的应该向我们展示代码...您使用调试器了吗??
-
您确定这些字符不在文件中吗?当它们作为文本查看时,它们可能不会出现,因为它们是不可打印的。
-
您是否使用
valgrind检查读取器和写入器是否存在缓冲区溢出?但要获得真正的帮助,您应该向我们展示真实的源代码。 -
你用十六进制编辑器检查过文本文件是否真的应该是这样的吗?