当未达到 EOF 时 feof() 返回 true

发布于 2024-10-07 06:26:33 字数 530 浏览 10 评论 0原文

我试图从特定偏移量的文件中读取(简化版本):

typedef unsigned char u8;
FILE *data_fp = fopen("C:\\some_file.dat", "r");
fseek(data_fp, 0x004d0a68, SEEK_SET); // move filepointer to offset
u8 *data = new u8[0x3F0];
fread(data, 0x3F0, 1, data_fp);
delete[] data;
fclose(data_fp);

问题是,该数据将不包含 1008 个字节,而是包含 529 个字节(似乎是随机的)。当达到 529 字节时,对 feof(data_fp) 的调用将开始返回 true。

我也尝试过读取较小的块(一次 8 个字节),但它看起来就像是在尚未到达 EOF 时那样。

在十六进制编辑器中简单查看一下即可发现还剩大量字节

I'm trying to read from a file at a specific offset (simplified version):

typedef unsigned char u8;
FILE *data_fp = fopen("C:\\some_file.dat", "r");
fseek(data_fp, 0x004d0a68, SEEK_SET); // move filepointer to offset
u8 *data = new u8[0x3F0];
fread(data, 0x3F0, 1, data_fp);
delete[] data;
fclose(data_fp);

The problem becomes, that data will not contain 1008 bytes, but 529 (seems random). When it reaches 529 bytes, calls to feof(data_fp) will start returning true.

I've also tried to read in smaller chunks (8 bytes at a time) but it just looks like it's hitting EOF when it's not there yet.

A simple look in a hex editor shows there are plenty of bytes left.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东北女汉子 2024-10-14 06:26:33

以文本模式打开文件(就像您所做的那样)会使库将某些文件内容转换为其他内容,可能会触发不必要的 EOF 或错误的偏移量计算。

通过将“b”选项传递给 fopen 调用以二进制模式打开文件

fopen(filename, "rb");

Opening a file in text mode, like you're doing, makes the library translate some of the file contents to other stuff, potentially triggering a unwarranted EOF or bad offset calculations.

Open the file in binary mode by passing the "b" option to the fopen call

fopen(filename, "rb");
清醇 2024-10-14 06:26:33

该文件是否正在被其他应用程序并行写入?也许存在竞争条件,因此当读取正在运行时,文件在读取停止的地方结束,但稍后当您检查它时,其余部分已被写入。这也可以解释随机性。

Is the file being written to in parallel by some other application? Perhaps there's a race condition, so that the file ends at wherever the read stops, when the read is running, but later when you inspect it the rest has been written. That would explain the randomness, too.

栀梦 2024-10-14 06:26:33

也许这是文本文件和二进制文件之间的区别。如果您使用的是 Windows,则换行符是 CRLF,它在文件中是两个字符,但在读取时仅转换为一个字符。尝试使用 fopen(..., "rb")

Maybe it's a difference between textual and binary file. If you're on Windows, newlines are CRLF, which is two characters in file, but converted to only one when read. Try using fopen(..., "rb")

牵你的手,一向走下去 2024-10-14 06:26:33

我看不到你的工作链接,但如果你的计算机声称不存在更多字节,我倾向于相信它。为什么不打印文件的大小而不是在十六进制编辑器中手动执行操作?

另外,你最好使用 2 级 I/O,f 调用是古老的 C 丑陋之处,而你正在使用 C++,因为你有新的。

int fh =open(filename, O_RDONLY);
struct stat s;
fstat(fh, s);
cout << "size=" << hex << s.st_size << "\n";

现在使用 2 级 I/O 调用进行查找和读取,无论如何,这种调用速度更快,让我们看看文件的实际大小是多少。

I can't see your link from work, but if your computer claims no more bytes exist, I'd tend to believe it. Why don't you print the size of the file rather than doing things by hand in a hex editor?

Also, you'd be better off using level 2 I/O the f-calls are ancient C ugliness, and you're using C++ since you have new.

int fh =open(filename, O_RDONLY);
struct stat s;
fstat(fh, s);
cout << "size=" << hex << s.st_size << "\n";

Now do your seeking and reading using level 2 I/O calls, which are faster anyway, and let's see what the size of the file really is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文