获取文件信息:C 语言详解152
简介
在 C 语言中,我们可以使用 stat() 函数获取有关文件的信息。该函数接收两个参数:要查询的文件路径和一个 stat 结构,用于存储结果信息。stat() 函数成功时返回 0,失败时返回 -1。
stat() 函数结构
stat() 函数的原型为:```c
int stat(const char *path, struct stat *buf);
```
其中:
path:要查询的文件路径
buf:指向将存储文件信息的 stat 结构的指针
stat() 结构
stat 结构包含有关文件的大量信息,包括:
文件类型(如常规文件、目录、链接)
文件大小
访问时间、修改时间和更改时间
用户和组 ID
权限
其他元数据
获取文件类型
要获取文件类型,可以使用 S_ISREG()、S_ISDIR() 和 S_ISLNK() 宏。例如:```c
if (S_ISREG(buf->st_mode)) {
printf("文件类型:常规文件");
} else if (S_ISDIR(buf->st_mode)) {
printf("文件类型:目录");
} else if (S_ISLNK(buf->st_mode)) {
printf("文件类型:符号链接");
}
```
获取文件大小
要获取文件大小,可以使用 st_size 字段。例如:```c
printf("文件大小:%ld 字节", buf->st_size);
```
获取访问时间
要获取文件最后访问的时间,可以使用 st_atime 字段。该字段以自纪元以来的秒数表示。要将其转换为可读格式,可以使用 ctime() 函数。例如:```c
time_t atime = buf->st_atime;
char *atime_str = ctime(&atime);
printf("最后访问时间:%s", atime_str);
```
获取修改时间
要获取文件最后修改的时间,可以使用 st_mtime 字段。该字段以自纪元以来的秒数表示。要将其转换为可读格式,可以使用 ctime() 函数。例如:```c
time_t mtime = buf->st_mtime;
char *mtime_str = ctime(&mtime);
printf("最后修改时间:%s", mtime_str);
```
获取更改时间
要获取文件最后更改的时间(元数据的更改时间),可以使用 st_ctime 字段。该字段以自纪元以来的秒数表示。要将其转换为可读格式,可以使用 ctime() 函数。例如:```c
time_t ctime = buf->st_ctime;
char *ctime_str = ctime(&ctime);
printf("最后更改时间:%s", ctime_str);
```
获取用户和组 ID
要获取文件的用户和组 ID,可以使用 st_uid 和 st_gid 字段。例如:```c
printf("用户 ID:%d", buf->st_uid);
printf("组 ID:%d", buf->st_gid);
```
获取权限
要获取文件的权限,可以使用 st_mode 字段。该字段包含一个位掩码,表示用户的权限、组权限和其他权限。我们可以使用 S_IRUSR、S_IWUSR 和 S_IXUSR 等宏来检查各个权限。例如:```c
if (buf->st_mode & S_IRUSR) {
printf("用户具有读取权限");
} else {
printf("用户没有读取权限");
}
```
获取其他元数据
stat() 结构还包含其他元数据,例如文件块大小、块数和设备 ID。这些信息可以使用相应的字段访问。
示例
以下代码演示了如何使用 stat() 函数获取文件信息:```c
#include
#include
#include
int main() {
char *path = "";
struct stat buf;
// 获取文件信息
if (stat(path, &buf) == -1) {
perror("stat() 失败");
return EXIT_FAILURE;
}
// 输出文件信息
printf("文件类型:");
if (S_ISREG(buf.st_mode)) {
printf("常规文件");
} else if (S_ISDIR(buf.st_mode)) {
printf("目录");
} else if (S_ISLNK(buf.st_mode)) {
printf("符号链接");
}
printf("文件大小:%ld 字节", buf.st_size);
// 转换访问时间到可读格式
time_t atime = buf.st_atime;
char *atime_str = ctime(&atime);
printf("最后访问时间:%s", atime_str);
// 转换修改时间到可读格式
time_t mtime = buf.st_mtime;
char *mtime_str = ctime(&mtime);
printf("最后修改时间:%s", mtime_str);
// 转换更改时间到可读格式
time_t ctime = buf.st_ctime;
char *ctime_str = ctime(&ctime);
printf("最后更改时间:%s", ctime_str);
printf("用户 ID:%d", buf.st_uid);
printf("组 ID:%d", buf.st_gid);
if (buf.st_mode & S_IRUSR) {
printf("用户具有读取权限");
} else {
printf("用户没有读取权限");
}
return EXIT_SUCCESS;
}
```
2024-11-13
上一篇:C 语言中的输出函数查找替换
Java方法栈日志的艺术:从错误定位到性能优化的深度指南
https://www.shuihudhg.cn/133725.html
PHP 获取本机端口的全面指南:实践与技巧
https://www.shuihudhg.cn/133724.html
Python内置函数:从核心原理到高级应用,精通Python编程的基石
https://www.shuihudhg.cn/133723.html
Java Stream转数组:从基础到高级,掌握高性能数据转换的艺术
https://www.shuihudhg.cn/133722.html
深入解析:基于Java数组构建简易ATM机系统,从原理到代码实践
https://www.shuihudhg.cn/133721.html
热门文章
C 语言中实现正序输出
https://www.shuihudhg.cn/2788.html
c语言选择排序算法详解
https://www.shuihudhg.cn/45804.html
C 语言函数:定义与声明
https://www.shuihudhg.cn/5703.html
C语言中的开方函数:sqrt()
https://www.shuihudhg.cn/347.html
C 语言中字符串输出的全面指南
https://www.shuihudhg.cn/4366.html