Ashish
Ashish

Reputation: 8529

How to get file size on disk on linux?

I want to find the size of file on disk on linux OS . I know command to do so: du -s -h

Is there any way to find it using c/c++ code ?

Upvotes: 12

Views: 18189

Answers (1)

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43558

Yes, use the stat(2) system call:

#include <sys/stat.h>
...
struct stat statbuf;

if (stat("file.dat", &statbuf) == -1) {
  /* check the value of errno */
}

printf("%9jd", (intmax_t) statbuf.st_size);

Upvotes: 18

Related Questions