简体   繁体   English

为什么 fread 总是返回 0?

[英]Why does fread always return 0?

I used this code to read file.我用这段代码来读取文件。 But fread function always return 0. What is my mistake?但是 fread function 总是返回 0。我的错误是什么?

FILE *file = fopen(pathToSourceFile, "rb");
if(file!=NULL) 
{
    char aByte[50000];
    int ret = fread(aByte, sizeof(aByte), 1, file);
    if(ret != 0)
    {
        not jump into there;
        fseek(file, 0, SEEK_SET);
        fwrite(aByte, ret, 1, file);
    }
} 
fclose(file); 

are you sure that your file has a size greater than 50000 ?您确定您的文件大小大于 50000 吗? otherwise you could try:否则你可以尝试:

 fread(aByte,1, sizeof(aByte),  file);

ferror() will tell when something is wrong. ferror()会告诉什么时候有问题。

You can print the actual error message using perror() .您可以使用perror()打印实际的错误消息。

You can't fwrite to a file open in rb mode.你不能fwrite到一个文件打开rb模式。

Your statement that ret is always zero is false.您关于ret始终为零的说法是错误的。 If you had properly instrumented your code, you'd not be making false claims:如果您正确检测了您的代码,您就不会做出虚假声明:

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *file = fopen("junk.dat", "rb");
    if(file!=NULL)
    {
        char aByte[50000];
        int ret = fread(aByte, sizeof(aByte), 1, file);
        fprintf(stderr, "fread returned %d\n", ret);

        if(ret != 0)
        {
            int fs = fseek(file, 0, SEEK_SET);
            if(fs == -1) {
                perror("fseek");
                exit(1);
            }
            fs = fwrite(aByte, ret, 1, file);
            if(fs != ret) {
                perror("fwrite");
                exit(1);
            }
        }
    }
    fclose(file);
    return 0;
}

Yields:产量:

fread returned 1
fwrite: Bad file descriptor

when run.运行时。

In case someone else runs into this.以防其他人遇到这种情况。 I just ran into a similar issue.我刚刚遇到了类似的问题。 It is because the 2nd argument to fread should be the size of each element in the buffer .这是因为 fread 的第二个参数应该是缓冲区每个元素的大小。 In OP's code it is the size of the pointer to the buffer.在 OP 的代码中,它是指向缓冲区的指针的大小。

This should work provided buff has at least 1 element:如果 buff 至少有 1 个元素,这应该可以工作:

int ret = fread(aByte, sizeof(aByte[0]), 1, file);

In my case I wanted to read a file of size 6553600 bytes (an mp3), and it was returning 0 bytes read.就我而言,我想读取一个大小为 6553600 字节(一个 mp3)的文件,它返回读取的 0 个字节。 It drove me crazy, till, I tried to manually hardcode 30 bytes, and it did read 30 bytes.它让我发疯,直到我尝试手动硬编码 30 个字节,它确实读取了 30 个字节。

I started playing with it and see how much can it read, and it turns out that it can read exactly 262144 (2^18) bytes, if you ask it to read 262145 bytes it reads 0.我开始玩它,看看它可以读取多少,结果它可以准确读取 262144 (2^18) 个字节,如果你要求它读取 262145 个字节,它读取的是 0。

Conclusion: at least with this function you can't load the whole file in one go.结论:至少使用此功能您无法一次性加载整个文件。

Please check man fread请检查 man fread

man fread(3)男人害怕(3)

size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream);

RETURN VALUE返回值

On success, fread() and fwrite() return the number of items read or written.成功时,fread() 和 fwrite() 返回读取或写入的项目数。 This number equals the number of bytes transferred only when size is 1 .只有当 size 为 1 时,此数字才等于传输的字节数。 If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).如果发生错误,或已到达文件末尾,则返回值为短项计数(或零)。

As your file is smaller than 50000Bytes aka.因为你的文件小于 50000Bytes aka。 size of a item, the read item count is 0.项的大小,读取的项数为 0。

In my case,就我而言,

    fseek(rFile, 0, SEEK_END);
    iTotalSize = ftell(rFile);
    fseek(rFile, 0, SEEK_SET);   // <-- I wrote SEEK_END, not SEEK_SET 

so it read 0 byte(anything)所以它读取 0 字节(任何东西)

Did you:你是否:

#include <unistd.h>

If not, and if you compile without -Wall, the C compiler can incorrectly assume that the second argument to fread() is an int rather than an off_t, which can mess up the function call.否则,如果您在没有 -Wall 的情况下进行编译,C 编译器可能会错误地假定 fread() 的第二个参数是 int 而不是 off_t,这可能会弄乱函数调用。 Your code snippet doesn't show any #include statements, so please make sure you're including everything that you're using.您的代码片段未显示任何 #include 语句,因此请确保包含您正在使用的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM