Linux编程:通过stat获取文件信息

91 篇文章 3 订阅 ¥99.90 ¥99.00

linux提供了stat函数,可以通过它获得文件的详细信息:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *statbuf);

参数说明:

pathname:文件的路径

statbuf:用于保存文件信息的结构

返回值:

0:成功

-1:失败,errno会设置具体的错误值

以下是struct stat结构体的说明:

struct stat {
    dev_t     st_dev;         /* ID of device containing file */文件使用的设备号
    ino_t     st_ino;         /* Inode number */ 索引节点号
    mode_t    st_mode;        /* File type and mode */文件类型和模式
    nlink_t   st_nlink;       /* Number of hard links */文件的硬连接数
    uid_t     st_uid;         /* User ID of owner */所有者的用户ID
    gid_t     st_gid;        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux下,可以使用C语言编程实现stat函数的功能。 stat函数用于获取文件的属性信息,包括文件类型、文件大小、创建时间、最后修改时间等。它的定义如下: ```c #include <sys/stat.h> int stat(const char *path, struct stat *buf); ``` 其中,path是要获取属性信息文件路径,buf是用于存储属性信息的结构体指针。 在调用stat函数之前,需要先定义一个结构体,用于存储属性信息。结构体的定义如下: ```c struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ 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; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system 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 */ }; ``` 其中,各个字段的含义如下: - st_dev:文件所在设备的ID。 - st_ino:文件的inode号。 - st_mode:文件的保护模式。 - st_nlink:文件的硬链接数。 - st_uid:文件所有者的用户ID。 - st_gid:文件所有者的组ID。 - st_rdev:如果文件是特殊文件,这里是设备ID。 - st_size:文件大小。 - st_blksize:文件系统I/O的块大小。 - st_blocks:512字节块的数量。 - st_atime:最后访问时间。 - st_mtime:最后修改时间。 - st_ctime:最后状态改变时间。 下面是一个示例程序,用于获取文件的属性信息并输出到屏幕上: ```c #include <stdio.h> #include <sys/stat.h> int main() { struct stat buf; int ret = stat("test.txt", &buf); if (ret == -1) { perror("stat"); return -1; } printf("File type: "); switch (buf.st_mode & S_IFMT) { case S_IFBLK: printf("block device\n"); break; case S_IFCHR: printf("character device\n"); break; case S_IFDIR: printf("directory\n"); break; case S_IFIFO: printf("FIFO/pipe\n"); break; case S_IFLNK: printf("symlink\n"); break; case S_IFREG: printf("regular file\n"); break; case S_IFSOCK: printf("socket\n"); break; default: printf("unknown?\n"); break; } printf("File size: %ld bytes\n", buf.st_size); printf("Number of blocks: %ld\n", buf.st_blocks); printf("Last status change time: %s", ctime(&buf.st_ctime)); printf("Last file access time: %s", ctime(&buf.st_atime)); printf("Last file modification time: %s", ctime(&buf.st_mtime)); return 0; } ``` 在这个示例程序中,我们使用了stat函数获取了test.txt文件的属性信息,并将这些信息输出到屏幕上。请注意,我们使用了ctime函数将时间戳转换为可读的时间格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风静如云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值