开发者

Why does fread always return 0?

I used this code to read file. But fread开发者_StackOverflow function always return 0. What is my mistake?

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 ? otherwise you could try:

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


ferror() will tell when something is wrong.

You can print the actual error message using perror().


You can't fwrite to a file open in rb mode.

Your statement that ret is always zero is false. 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 my case I wanted to read a file of size 6553600 bytes (an mp3), and it was returning 0 bytes read. It drove me crazy, till, I tried to manually hardcode 30 bytes, and it did read 30 bytes.

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.

Conclusion: at least with this function you can't load the whole file in one go.


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. In OP's code it is the size of the pointer to the buffer.

This should work provided buff has at least 1 element:

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


Please check man fread

man fread(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. This number equals the number of bytes transferred only when size is 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. size of a item, the read item count is 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. Your code snippet doesn't show any #include statements, so please make sure you're including everything that you're using.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜