How does fread know when the file is over in C?
That's not how you properly read from a file in C.
fread
returns a size_t
representing the number of elements read successfully.
FILE* file = fopen(filename, "rb");
char buffer[4];
if (file) {
/* File was opened successfully. */
/* Attempt to read */
while (fread(buffer, sizeof *buffer, 4, file) == 4) {
/* byte swap here */
}
fclose(file);
}
As you can see, the above code would stop reading as soon as fread
extracts anything other than 4 elements.