When reading in a file, it is often useful to know its size without relying on the EOF (for reasons Klausi mentioned). Here is a simple way to find filesize on a POSIX system:
I'm not checking for errors for the sake of clarity, but make sure you do so in your application :) stat() will return -1 and set errno if something goes wrong.
struct stat buf;
const char *path = "/home/jdeckard/testfile";
stat( path, &buf );
filesize = buf.st_size;
Here is a less efficient, yet ANSI compliant, solution:
If you decide not to close your file right away, keep in mind that the file descriptor refers to the end of the file. You will want to move it before doing any reads.
fptr = fopen( "/home/jdeckard/testfile", "r" );
fseek( fptr, 0, SEEK_END );
filesize = ftell( fptr );
fclose( fptr );