一、整体大纲

Linux C编程之八 文件操作相关函数

st_mode整体介绍:

Linux C编程之八 文件操作相关函数

st_mode详细介绍:

Linux C编程之八 文件操作相关函数

二、 Linux文件操作相关函数

1. stat

  • 作用:获得文件信息,也可以获取文件大小。
  • 头文件
#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);
  • 参数说明:

           path文件名
           buf传出参数,定义结构体struct stat sb; &sb

  • 返回值

          失败:返回-1,设置errno
          成功:返回0

注意: stat碰到链接,会追溯到源文件,穿透!!!lstat并不会穿透。

stat结构体:
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 */
};

linux命令stat执行结果:

[root@centos linuxC]# stat stat.c
  文件:"stat.c"
  大小:1857            块:8          IO 块:4096   普通文件
设备:fd00h/64768d      Inode:17763202    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:usr_t:s0
最近访问:2019-04-27 19:17:15.149083960 +0800
最近更改:2019-04-27 19:17:15.149083960 +0800
最近改动:2019-04-27 19:17:15.202084912 +0800

注意三个时间的区别:

time_t    st_atime;   /* time of last access */ 文件被读,比如cat,open读等
time_t    st_mtime;   /* time of last modification */  文件内容发生改变
time_t    st_ctime;   /* time of last status change */ 文件属性发生变化,比如大小,权限,硬连接数等

Linux C编程之八 文件操作相关函数

上图的解释:

0-2 其他用户权限
3-5 组用户权限
6-8 用户权限
9-11 特殊权限位
12-15 文件类型

示例:

 1 #include <sys/types.h>
 2 #include <sys/stat.h>
 3 #include <time.h>
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 
 7 int
 8 main(int argc, char *argv[])
 9 {
10     struct stat sb;
11 
12     if (argc != 2) {
13         fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
14         exit(EXIT_FAILURE);
15     }
16 
17     if (stat(argv[1], &sb) == -1) {
18         perror("stat");
19         exit(EXIT_FAILURE);
20     }
21 
22     printf("File type:                ");
23 
24     switch (sb.st_mode & S_IFMT) {
25     case S_IFBLK:  printf("block device\n");            break;
26     case S_IFCHR:  printf("character device\n");        break;
27     case S_IFDIR:  printf("directory\n");               break;
28     case S_IFIFO:  printf("FIFO/pipe\n");               break;
29     case S_IFLNK:  printf("symlink\n");                 break;
30     case S_IFREG:  printf("regular file\n");            break;
31     case S_IFSOCK: printf("socket\n");                  break;
32     default:       printf("unknown?\n");                break;
33     }
34     printf("I-node number:            %ld\n", (long) sb.st_ino);
35 
36     printf("Mode:                     %lo (octal)\n",
37             (unsigned long) sb.st_mode);
38 
39     printf("Link count:               %ld\n", (long) sb.st_nlink);
40     printf("Ownership:                UID=%ld   GID=%ld\n",
41             (long) sb.st_uid, (long) sb.st_gid);
42 
43     printf("Preferred I/O block size: %ld bytes\n",
44             (long) sb.st_blksize);
45     printf("File size:                %lld bytes\n",
46             (long long) sb.st_size);
47     printf("Blocks allocated:         %lld\n",
48             (long long) sb.st_blocks);
49 
50     printf("Last status change:       %s", ctime(&sb.st_ctime));
51     printf("Last file access:         %s", ctime(&sb.st_atime));
52     printf("Last file modification:   %s", ctime(&sb.st_mtime));
53 
54     exit(EXIT_SUCCESS);
55 }
Linux自带示例(打印文件相关信息)

相关文章:

  • 2022-12-23
  • 2021-09-12
  • 2021-08-30
  • 2021-08-26
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-02-14
猜你喜欢
  • 2021-09-16
  • 2021-07-13
  • 2021-07-02
  • 2021-10-17
  • 2021-04-22
  • 2021-06-09
  • 2021-09-19
相关资源
相似解决方案