如何在Linux上的磁盘上获取文件大小?

时间:2011-04-26 15:59:42

标签: c++ c linux

我想在linux OS上找到磁盘上文件的大小。 我知道这样做的命令: du -s -h

有没有办法使用c / c ++代码找到它?

1 个答案:

答案 0 :(得分:17)

是的,使用stat(2)系统调用:

#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);
相关问题