10

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); 
13
  • 2
    Does the file definitely have at least 50000 bytes? Commented Aug 29, 2010 at 11:10
  • 1
    @Charles, it shouldn't need to. fread should return the number of bytes actually read, which may be less than 50000. Commented Aug 29, 2010 at 11:12
  • 12
    @Matthew Flaschen: No. fread returns the number of objects read. If you are reading up to one 50000 byte objects it will return 0 or 1. Commented Aug 29, 2010 at 11:14
  • 5
    Can everyone please stop removing the C++ tag from this question. It is obviously the language that that user418304 wants to use. Just because the code is also valid C doesn't mean that the question is about C. (People don't retag C questions as C++ just because the code uses the common subset, do they?) Commented Aug 29, 2010 at 11:25
  • 2
    @msw Please don't try to justify making fun of the OP with wanting to help them or SO. This kind of behaviour is neither funny nor helping anybody. Commented Aug 29, 2010 at 12:46

8 Answers 8

10

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

 fread(aByte,1, sizeof(aByte), file); 
Sign up to request clarification or add additional context in comments.

1 Comment

YES because i readed data from a mp3 file that the size on disk is 3.2mb :S
3

ferror() will tell when something is wrong.

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

1 Comment

ferror() won't tell you what is wrong, it'll tell you if something is wrong - it's just a true/false indication. Using perror() if ferror() returns true is good advice.
2

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.

Comments

1

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.

2 Comments

Yes sorry i change to rb to test fread. please skip to fwrite. :)
It is best to cut and paste the code that you are actually having problems with end you can edit your question if your code changes.
1

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)

Comments

0

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

Comments

0

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.

Comments

-1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.