Is the following the correct way to get the number of bytes in a file? Are there more straightforward ways, perhaps directly from the FILE*
object?
size_t
num_bytes(FILE *fp)
{
fseek(fp, 0, SEEK_END);
return (size_t) ftell(fp);
}
Additionally, why does ftell
return a long int
and not a size_t
object? I find it a bit difficult to figure out which "size-ish" functions return size_t
and which don't. Is there a good way to remember that? Or is the reason that ftell
isn't size_t
because it uses -1L
as the "fail value" ?