c - 从文本文件读取: strange "\x01\0\0\0" sequences

标签 c xcode file text stream

我已经为这个问题苦苦挣扎了一个星期,但离解决方案还很远。

我正在通过字符串从文件中读取纯文本。它成功读取所有内容,直到具有一定大数字的字符。 这个数字对于每个文件来说都是特定的,并且对于不同的文件来说是不同的。

到达如此大数字的字符后,它会读取“\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.

最佳答案

正如我在评论中建议的:

  • 您应该将源代码放在问题中(您可以随时编辑以改进它),而不是放在注释中
  • 您应该使用 POSIX 2008 getline在您的 FILE* 上运行。
  • 您应该稍后通过getline隐式释放缓冲区malloc,例如当fclose-ing你的FILE*句柄时。
  • 您应该学习使用像 gdb 这样的调试器
  • 您应该学习使用内存泄漏检测器,例如 valgrind

关于c - 从文本文件读取: strange "\x01\0\0\0" sequences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9553978/

相关文章:

ios - Apple Mach-O 链接器错误。 ID : file not found. .. .app

ios - 在 iPhone 上运行分割 View 应用程序时 Xcode 9 崩溃

Java-如何获取文件夹中的所有文件名

c - 文件访问中的段错误

c - 释放 `struct` 中的指针用作 `%union` 的模型

c - Linux服务器--用鼠标选择任何内容后$后面出现 "^C"

c - Gtk-关键 ** : IA__gtk_widget_get_parent: assertion `GTK_IS_WIDGET (widget)' failed

C pthread : how to activate one thread when a condition is met?

Java 文件更新

ios - UIMenuController 和 TapGesture 在移动时重新出现