使用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;

}

编译运行:

相关推荐
土司大王2 小时前
LeetCode hot100——对称二叉树
算法·leetcode·职场和发展
智恒百亿2 小时前
RTX 5090 全场景技术应用解析:从游戏算力到专业生产力落地
大数据·服务器·游戏
2401_868534782 小时前
指令集-最常用 Linux 命令速查手册(整合版)
linux·网络协议
ltl2 小时前
小文件问题:文件数量如何放大元数据与 I/O 成本
linux
sali-tec2 小时前
C# 基于OpenCv的视觉工作流-章106-差值追踪
图像处理·人工智能·opencv·算法·计算机视觉
Navigator_Z2 小时前
LeetCode //C - 1208. Get Equal Substrings Within Budget
c语言·算法·leetcode
ltl3 小时前
网络延迟优化:Nagle、TCP_NODELAY 与中断亲和
linux
OpenCloudOS3 小时前
从快速响应到问题发现:一次backport挖出全新漏洞CVE-2026-76641
服务器·网络·安全
手写码匠3 小时前
Dify 多 Agent 工具权限与安全沙箱实战:让智能体“有能力,但不越权“
人工智能·深度学习·算法·aigc