miwaiwai

导航

C语言学习

文件系统:

  1.inode

    本质是结构体,

    存储文件属性信息:

        1.权限

        2.类型

        3.大小

        4.时间

        5.用户

        6.盘块位置

 

  2.dentry

stat函数:

  int stat(const char *path,struct stat *buf)

  参数:

    path:文件路径

    buf:(穿书参数)存放文件的路径

  返回值:

    成功:0

    失败:-1 errno

通过stat函数获取文件大小和时间

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <sys/stat.h>
 5 int main(int argc,char *argv[]){
 6 struct stat sbuf;
 7 int ret=stat(argv[1],&sbuf);
 8 if(ret==-1){
 9     perror("stat error");
10     exit(1);
11 
12 }
13 printf("file size:%ld\n",sbuf.st_atime);
14 printf("file size:%ld\n",sbuf.st_size);
15 return 0;
16 }

 

通过stat函数获取文件类型,遍历目录,下面这个例子,在man配置里面有举例

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<unistd.h>
 5 #include<pthread.h>
 6 #include<dirent.h>
 7 int main (int argc, char *argv[]) {
 8 
 9     DIR *dp;
10     struct dirent *sdp;
11     if (argc < 2) {
12         printf("usage error!");
13         exit (1);
14     }
15     dp = opendir (argv[1]);
16     if (dp == NULL) {
17 
18         perror ("opendir error");
19         exit (1);
20     }
21     while ((sdp = readdir (dp)) != NULL) {
22         if ((strcmp (sdp->d_name, ".") == 0) || (strcmp (sdp->d_name, "..") == 0)) {
23             continue;
24         }
25         printf("%s\t", sdp->d_name);
26     }
27     printf("\n");
28     closedir (dp);
29 
30     return 0;
31 }

 

目录操作函数:

  opendir()

  closedir()

  readdir()

递归遍历目录:ls -R .c

  1.判断命令行参数,获取用户要查询的目录名. argv[1]

    argc==1 ---> ./

  2.判断用户指定的是否是目录. stat S_IDDIR()---->封装函数isFile

  3.读目录

    opendir(dir)

    while(readdir()){

      普通文件,直接打印

      目录:拼接目录访问绝对路径.sprintf (path,""%s%s"",dir,d_name)

        递归调用自己.--->opendir readdir closedir

    }

    closedir()

 

posted on 2022-11-04 20:17  米歪歪  阅读(39)  评论(0)    收藏  举报