C 语言获取文件 inode 号码示例代码
#include<sys/types.h> #include<sys/stat.h> #include<unistd.h>
#include<stdio.h> #include<stdlib.h>
ino_t get_inode(char *fname){ struct stat info;
if(stat(fname, &info) == -1){ printf("cannot stat"); exit(1); }
return info.st_ino;
}
int main(int argc, char* argv[]) { if(argc != 2){ printf("usage: %s filename\n", argv[0]); exit(1); }
printf("inode of %s: %lu\n", argv[1], get_inode(argv[1]));
return 0;
}
在补全的代码中,主要是增加了对命令行参数的检查,确保程序可以正确接收用户输入的文件名。同时,在输出时,使用了正确的格式化字符,确保输出的是无符号长整型数值,而不是整型数值。
原文地址: https://www.cveoy.top/t/topic/n4l4 著作权归作者所有。请勿转载和采集!