如何在linux上获取磁盘上的文件大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5793030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:50:00 来源:igfitidea点击:
How to get file size on disk on linux?
提问by Ashish
I want to find the size of file on disk on linux OS . I know command to do so: du -s -h
我想在 linux OS 上找到磁盘上的文件大小。我知道这样做的命令:du -s -h
Is there any way to find it using c/c++ code ?
有没有办法使用 c/c++ 代码找到它?
采纳答案by Blagovest Buyukliev
Yes, use the stat(2)
system call:
是的,使用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);