'Why does fread always return 0?

I used this code to read file. But fread 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); 


Solution 1:[1]

are you sure that your file has a size greater than 50000 ? otherwise you could try:

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

Solution 2:[2]

ferror() will tell when something is wrong.

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

Solution 3:[3]

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.

Solution 4:[4]

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);

Solution 5:[5]

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.

Solution 6:[6]

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.

Solution 7:[7]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4 Tom Florkiewicz
Solution 5 Tudor
Solution 6 Thermit
Solution 7