How do you get file size by fd?

R__ picture R__ · Jun 30, 2011 · Viewed 26.6k times · Source

I know I can get file size of FILE * by fseek, but what I have is just a INT fd.

How can I get file size in this case?

Answer

Hasturkun picture Hasturkun · Jun 30, 2011

You can use lseek with SEEK_END as the origin, as it returns the new offset in the file, eg.

off_t fsize;

fsize = lseek(fd, 0, SEEK_END);