stat 文件_readlink函数
文件 函数 stat
2023-06-13 09:11:03 时间
stat
stat函数主要用于获取文件的inode信息。 stat命令其实就是调用的stat函数。
stat中时间的辨析
- atime(最近访问时间)
- mtime(最近更改时间):指最近修改文件内容的时间
- ctime(最近改动时间):指最近改动inode的时间
1)chmod 777 stat_1.txt
stat之后发现ctime变了。改变了文件的权限,文件权限保存在inode里面。
2)vim stat_1.txt
什么都不做,看一下退出。stat后发现atime变了。
3)echo "123456" >>stat_1.txt
追加到stat_1.txt后发现mtime,ctime变了。文件内容变了mtime改变,文件大小变了ctime改变,文件大小保存在inode里面。
用法:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
fstat函数获得已在描述符上打开的文件的有关信息。 lstat函数类似于stat,但是当命名的文件是一个符号连接时, lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息。
函数说明: 通过path获取文件信息,并保存在buf所指的结构体stat中。
返回值: 执行成功则返回0,失败返回-1,错误代码存于errno。 stat结构体:
struct stat {
dev_t st_dev; /* 文件的设备编号 */
ino_t st_ino; /* inode number */
mode_t st_mode; /* 文件的类型和存取权限 */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* 文件字节数 */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
实例:
1)输出文件的大小。
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char* argv[])
{
struct stat s_buf;
if(argc < 2)
{
printf("./app filename\n");
exit(1);
}
if(stat(argv[1], &s_buf) < 0)
{
perror("stat");
exit(1);
}
printf("%s\t%d\n", argv[1], s_buf.st_size);
return 0;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171798.html原文链接:https://javaforall.cn
相关文章
- 项目差异class文件提取–>上线用
- Java上传文件到数据库「建议收藏」
- fopen函数打开文件失败原因_为什么打开文件失败
- win7-64 隐藏文件,进程,注册表
- 【C 语言】文件操作 ( 按照单个字符的方式读写文件 | fgetc 函数 | fputc 函数 )
- 【C 语言】文件操作 ( 文件加密解密 | 加密解密原理 | 对称加密 | 非对称加密 | 散列函数 )
- 使用Mavne生成可以执行的jar文件详解程序员
- Python os.stat()函数:获取文件基本信息
- 文件Linux函数集:CHM格式指南(linux函数手册chm)
- 及作用【Oracle文件后缀名及其功能介绍】(oracle后缀名)
- 夹Linux挂载共享文件夹:实现云端同步(linux挂载共享文件)
- Linux系统快速恢复覆盖已有文件(linux 恢复覆盖文件)
- 文件遍历排序函数
- 读写xml文件的2个小函数
- CheckFile函数之asp实现检查某一文件是否存在的代码
- asp下利用fso实现文件夹或文件移动改名等操作函数
- python文件和路径操作函数小结
- php目录操作函数之获取目录与文件的类型
- PHP备份数据库生成SQL文件并下载的函数代码
- PHP文件去掉PHP注释空格的函数分析(PHP代码压缩)
- php打开文件fopen函数的使用说明
- 自己写了一个php检测文件编码的函数
- jquery动态加载js/css文件方法(自写小函数)
- python文件和目录操作函数小结