Reputation: 137
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);
Upvotes: 10
Views: 27285
Reputation: 169
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)
Upvotes: 1
Reputation: 66
Please check man fread
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.
Upvotes: 0
Reputation: 35246
are you sure that your file has a size greater than 50000 ? otherwise you could try:
fread(aByte,1, sizeof(aByte), file);
Upvotes: 10
Reputation: 2706
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.
Upvotes: 2
Reputation: 642
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);
Upvotes: 0
Reputation: 1157
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.
Upvotes: -1
Reputation: 141790
ferror()
will tell when something is wrong.
You can print the actual error message using perror()
.
Upvotes: 3
Reputation: 43487
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.
Upvotes: 1