Reputation: 8529
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
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