使用stat()函数的例子

代码:

复制代码
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>



int main(void)
{
	struct stat st;
	if(-1==stat("test.txt",&st))
	{
		printf("获得文件状态失败\n");
		return -1;
	}
	printf("包含此文件的设备ID: %d\n",st.st_dev);
	printf("此文件的节点: %d\n",st.st_ino);
	printf("此文件的保护模式: %d\n",st.st_mode);
        printf("此文件的硬件链接数: %d\n",st.st_nlink);
	printf("此文件的所有者ID: %d\n",st.st_uid);
        printf("此文件的所有者的组ID: %d\n",st.st_gid);

        printf("设备ID (如果此文件为特殊设备): %d\n",st.st_rdev);
        printf("此文件的大小: %d\n",st.st_size);
        printf("此文件的所在文件系统块大小: %d\n",st.st_blksize);
	printf("此文件的占用块数量: %d\n",st.st_blocks);
        printf("此文件的最后访问时间: %d\n",st.st_atime);
        printf("此文件的最后修改时间: %d\n",st.st_mtime);
        printf("此文件的最后状态改变时间: %d\n",st.st_ctime);

	return 0;

}

编译运行:

相关推荐
菜鸡爱玩3 小时前
线性代数矩阵相乘
线性代数·算法·矩阵
devilnumber7 小时前
Java 递归算法 详解 + 核心要点 + 实战运用 + 避坑指南
java·开发语言·算法
kebidaixu7 小时前
BCU 平台 RS485 驱动适配:从 THVD1406 到 ISO3082
linux
‎ദ്ദിᵔ.˛.ᵔ₎9 小时前
双指针、滑动窗口、前缀和、二分查找 算法
算法
顾北顾9 小时前
多头注意力机制
人工智能·深度学习·算法
H178535090969 小时前
SolidWorks_基于草图的实体特征20_特征错误排查
算法·3d建模·solidworks
hujinyuan201609 小时前
2025年12月中国电子学会青少年机器人技术等级考试试卷(二级) 真题+答案
人工智能·算法·机器人
映翰通朱工9 小时前
工业4G网关无公网IP远程运维实战(内网终端异地访问方案)
运维·服务器·网络·安全·智能路由器
洪晓露9 小时前
将 rke2 集群证书延长至 10 年
运维·服务器·数据库
谢平康10 小时前
解决用 rm 报bash: /usr/bin/rm: Argument list too long错
linux·运维·运维开发