fread() does not reach EOF in C
Hi, this is a beginner question.
Code:
#include <stdint.h> Thanks. |
Your code assumes that fread returns the value of the byte read, which could be EOF. It doesn't. It returns the number of "blocks" read.
So it should be: Code:
while (fread(buffer, BLOCK, 1, file)) { |
or for more readable
Code:
while (fread(buffer, BLOCK, 1, file) > 0) |
Quote:
Code:
RETURN VALUE Hope this points to a solution... |
Reply #2 uses the fact that "zero is 'false-y'" and thus ends the while loop. This is very commonly done. But, the equivalent reply #3 is more explicit.
|
Another solution is to find out the size of the file and read the entire file in 1 shot.
Code:
int fileSize; |
All times are GMT -5. The time now is 01:47 AM. |