• 目录IO及小练习


    一、目录操作函数

    1. opendir  打开目录
    2. readdir  读目录文件
    3. closedir 关闭目录
    4. chdir 修改当前所处路径

    1、打开目录:opendir

    1. #include 
    2. #include 
    3.   DIR *opendir(const char *name);
    4.   功能:打开目录文件
    5.   参数:name:文件名
    6. 返回值:成功返回目录流指针,失败NULL,更新error

    2、closedir 关闭目录2

    1. #include 
    2. #include 
    3. int closedir(DIR *dirp);
    4.    功能:关闭目录
    5.    参数:dirp:目录流指针
    6.    返回值:成功0,失败-1,更新errno

    3、readdir  读目录文件

    1.   #include <sys/types.h>
    2.   #include <dirent.h>
    3.   struct dirent *readdir(DIR *dirp);
    4.     功能:读目录文件
    5.     参数:dirp:目录流指针
    6.     返回值:成功返回结构体指针,读到文件结尾返回NULL
    7.          失败NULL,更新errno
    8.  struct dirent 
    9.      {
    10.      ino_t          d_ino;       /*文件的inode */
    11.      off_t          d_off;       /* not an offset; see NOTES */
    12.      unsigned short d_reclen;    /* 记录的长度 */
    13.      unsigned char  d_type;      /* 文件类型,并不支持所有文件类型*/
    14.      char           d_name[256]; /* filename */
    15. }       

    练习实现ls功能

    1. #include<stdio.h>
    2. #include <sys/types.h>
    3. #include <dirent.h>
    4. int main(int argc, char const *argv[])
    5. {
    6.     struct dirent *dir;//创建结构体
    7.     DIR *dirp = opendir(argv[1]);//打开文件
    8.     if(dirp == NULL)//容错
    9.     {
    10.         perror(" opendir err");
    11.         return -1;
    12.     }
    13.     while ((dir = readdir(dirp)) != NULL)//读取目录文件
    14.     {
    15. if(dir->d_name[0]=='.')
    16. continue;
    17.         printf("%s ",dir->d_name);//输出目录内容
    18.     }
    19.     puts(" ");
    20.     closedir(dirp);//关闭文件目录流
    21.     return 0;
    22. }

    4、chdir 修改当前所处路径

    1. #include 
    2. int chdir(const char *path);
    3.      功能:改变当前所处的路径
    4.  参数:path:修改的路径、
    5.  返回值:成功0
    6.       失败:-1,更新errno

    二、获取文件属性函数stat

    1. stat :显示文件或文件系统状态
    2. #include <sys/types.h>
    3. #include <sys/stat.h>
    4. #include <unistd.h>
    5. int stat(const char *pathname, struct stat *buf);
    6. 功能:获取文件属性
    7. 参数:pathname:文件路径名
    8.        buf:保存文件属性信息的结构体
    9. 返回值:成功:0
    10.       失败:-1 更新error
    11. struct stat 
    12. {
    13.         dev_t     st_dev;         /* 设备包含文件ID */
    14.         ino_t     st_ino;     /* inode号 */
    15.         mode_t    st_mode;    /* 文件类型和权限 */
    16.         nlink_t   st_nlink;   /* 硬链接数 */
    17.         uid_t     st_uid;     /* 用户ID */
    18.         gid_t     st_gid;     /* 组ID */
    19.         off_t     st_size;    /* 大小 */
    20.         dev_t     st_rdev;        /* 设备ID */
    21.         time_t    st_atime;   /* 最后访问时间 */
    22.         time_t    st_mtime;   /* 最后修改时间 */
    23.         time_t    st_ctime;  /* 最后状态改变时间 */
    24.     };
    25.  st_mode 包含的是文件类型和权限:
    26.     文件类型:
    27. S_IFMT     0170000   bit mask for the file type bit field
    28. S_IFSOCK   0140000   socket-套接字 S
    29.  S_IFLNK    0120000   symbolic link-连接文件L
    30.  S_IFREG    0100000   regular file-普通文件 -
    31.  S_IFBLK    0060000   block device块设备b
    32.  S_IFDIR    0040000   directory目录 d
    33.  S_IFCHR    0020000   character 字符设备 c
    34.  S_IFIFO    0010000   FIFO管道 p
    35.  使用规则:
    36.  stat(pathname, &sb);
    37. if ((sb.st_mode & S_IFMT) == S_IFREG) {
    38.     /* Handle regular file */
    39. }
    40.     将使用规则封装:封装成宏函数
    41.   S_ISREG(m)  is it a regular file?
    42.   S_ISDIR(m)  directory?
    43.   S_ISCHR(m)  character device?
    44.   S_ISBLK(m)  block device?
    45.   S_ISFIFO(m) FIFO (named pipe)?
    46.   S_ISLNK(m)  symbolic link?  (Not in POSIX.1-1996.)
    47.   S_ISSOCK(m) socket?  (Not in POSIX.1-1996.)
    48. 权限:后九位控制权限
    49.  S_IRUSR     00400   owner has read permission
    50.  S_IWUSR     00200   owner has write permission
    51.  S_IXUSR     00100   owner has execute permission
    52.  S_IRGRP     00040   group has read permission
    53.  S_IWGRP     00020   group has write permission
    54.  S_IXGRP     00010   group has execute permission
    55.  S_IROTH     00004   others have read permission
    56.  S_IWOTH     00002   others have write permission
    57.  S_IXOTH     00001   others have execute permission
    58.  1.getpwuid:通过用户id获取用户名
    59.  struct passwd *getpwuid(uid_t uid);
    60.  struct passwd 
    61. {
    62.         char   *pw_name;       /* username */
    63.         char   *pw_passwd;     /* user password */
    64.         uid_t   pw_uid;        /* user ID */
    65.         gid_t   pw_gid;        /* group ID */
    66.         char   *pw_gecos;      /* user information */
    67.         char   *pw_dir;        /* home directory */
    68.         char   *pw_shell;      /* shell program */
    69.  };
    70.   2.getgrgid:通过组id获取组名
    71.   struct group *getgrgid(gid_t gid);
    72.   struct group 
    73.   {
    74.       char   *gr_name;       /* group name */
    75.       char   *gr_passwd;     /* group password */
    76.       gid_t   gr_gid;        /* group ID */
    77.       char  **gr_mem;        /* group members */
    78.   };  
    79. 3. ctime();将时间转转换为字符串表示格式
    80. char *ctime(const time_t *timep);
    81. 如:转换为-"Wed Jun 30 21:49:08 1993\n"

    练习:用目录IO函数实现ls -l 和ls -i功能

    1. #include<stdio.h>
    2. #include <sys/types.h>
    3. #include <sys/stat.h>
    4. #include <unistd.h>
    5. #include <pwd.h>
    6. #include <grp.h>
    7. #include<time.h>
    8. #include <dirent.h>
    9. #include<string.h>
    10. void Show(char *file)
    11. {
    12.     struct stat sb;
    13.     stat(file,&sb);
    14.     switch(sb.st_mode & __S_IFMT)
    15.     {
    16.         case __S_IFSOCK:printf("s");break;
    17.         case __S_IFLNK:printf("l");break;
    18.         case __S_IFREG:printf("-");break;
    19.         case __S_IFBLK:printf("b");break;
    20.         case __S_IFDIR:printf("d");break;
    21.         case __S_IFCHR:printf("c");break;
    22.         case __S_IFIFO:printf("p");break;
    23.         default:break ;
    24.     }
    25.     //1
    26.     // char bus[3]="rwx";
    27.     // for(int i=0;i<9;i++)
    28.     // sb.st_mode & (0400>>i)?printf("%c",bus[i%3]):printf("-");
    29.     //2
    30.     for(int i = 8;i>=0;i--)
    31.     {
    32.         if(sb.st_mode & (1<<i))
    33.         {
    34.             if(i == 8||i == 5||i == 2)
    35.             printf("r");
    36.             if(i == 7||i == 4||i == 1)
    37.             printf("w");
    38.             if(i == 6||i == 3||i == 0)
    39.             printf("x");
    40.         }
    41.         else
    42.         {
    43.             printf("-");
    44.         }
    45.     }
    46.     #if 0//3
    47.     //用户权限
    48.     if(sb.st_mode & (1<<8))
    49.     printf("r");
    50.     else
    51.     {
    52.         printf("-");
    53.     }
    54.     if(sb.st_mode & (1<<7))
    55.     printf("w");
    56.     else
    57.     {
    58.         printf("-");
    59.     }
    60.     if(sb.st_mode & (1<<6))
    61.     printf("x");
    62.     else
    63.     {
    64.         printf("-");
    65.     }
    66.         //组用户权限
    67.     if(sb.st_mode & (1<<5))
    68.     printf("r");
    69.     else
    70.     {
    71.         printf("-");
    72.     }
    73.     if(sb.st_mode & (1<<4))
    74.     printf("w");
    75.     else
    76.     {
    77.         printf("-");
    78.     }
    79.     if(sb.st_mode & (1<<3))
    80.     printf("x");
    81.     else
    82.     {
    83.         printf("-");
    84.     }
    85.         //其他用户权限
    86.     if(sb.st_mode & (1<<2))
    87.     printf("r");
    88.     else
    89.     {
    90.         printf("-");
    91.     }
    92.     if(sb.st_mode & (1<<1))
    93.     printf("w");
    94.     else
    95.     {
    96.         printf("-");
    97.     }
    98.     if(sb.st_mode & (1<<0))
    99.     printf("x");
    100.     else
    101.     {
    102.         printf("-");
    103.     }
    104.     #endif
    105.     printf(" %u",sb.st_nlink);
    106.     struct passwd *uname = getpwuid(sb.st_uid);
    107.     struct group *gname = getgrgid(sb.st_gid);
    108.     printf(" %s",gname->gr_name);//用户名
    109.     printf(" %s",uname->pw_name);//组用户名
    110.     printf(" %ld",sb.st_size);
    111.     printf(" %.12s",4+ctime(&sb.st_mtime));
    112.     printf(" %s",file);
    113.     puts(" ");
    114. }
    115. int main(int argc, char const *argv[])
    116. {
    117.     struct dirent *dir;//创建结构体
    118.     DIR *dirp = opendir("./");//打开文件
    119.     if(dirp == NULL)//容错
    120.     {
    121.         perror(" opendir err");
    122.         return -1;
    123.     }
    124.     if(!strncmp(argv[1],"-l",2))//判断输入的命令
    125.     {
    126.         while ((dir = readdir(dirp)) != NULL)//读取目录文件
    127.         {
    128.             if(dir->d_name[0]=='.')
    129.             continue;
    130.             Show(dir->d_name);
    131.         }
    132.     }
    133.     else if (!strncmp(argv[1],"-i",2))
    134.     {
    135.         struct stat sb;
    136.         stat(dir->d_name,&sb);
    137.             while ((dir = readdir(dirp)) != NULL)//读取目录文件
    138.         {
    139.             if(dir->d_name[0]=='.')
    140.             continue;
    141.             stat(dir->d_name,&sb);
    142.             printf("%-7ld",sb.st_ino);
    143.             printf("%-13s",dir->d_name);//输出目录内容
    144.         }
    145.         puts(" ");
    146.     }
    147.     closedir(dirp);//关闭文件目录流
    148.     return 0;
    149. }

  • 相关阅读:
    K_A02_003 基于单片机驱动8位数码管模块(MAX7219) 0-7静态显示+滚动显示
    TensorFlow是什么
    人工智能申报!武汉市人工智能创新专项项目申报要求、流程和申报限制
    【iOS】引用计数
    由于没有远程桌面授权服务器可以提供许可证,远程会话连接已断开
    Java 8 给我们更好的消灭空指针解决方案
    常见Python面试题整理带答案
    python在循环中捕获异常后继续执行下一轮
    简单工厂模式概述和使用
    输出格式 && 常用的运算符
  • 原文地址:https://blog.csdn.net/m0_74937538/article/details/134042778