十五、文件和目录——目录操作函数

15.1 目录操作函数

15.1.1 创建目录

1 #include <sys/types.h>  
2 #include <sys/stat.h>  
3 int mkdir(const char *pathname, mode_t mode)
  • 函数说明:
    • 该函数创建一个名为pathname的空目录,此目录自动含有 “.” 和 “..” 2 个登记项。这个新创建目录的用户 ID 被设置为调用进程的有效用户 ID,其组则为父目录的组 ID 或者进程的有效组 ID
    • 若调用成功,mkdir 将更新该目录的 st_atime、st_ctime 和 st_mtime,同时更新其父目录的 st_ctime 和 st_mtime ,然后返回 0 。若调用失败,mkdir 将返回-1.
    • 由 pathname指定的新目录的父目录必须存在,并且调用进程必须具有该父目录的写权限以及 pathname 涉及的各个分路径目录的搜寻权限
    • 创建目录时,至少指定一个执行权限位
  • 函数功能:创建目录
  • 函数参数:
    • @pathname : 新创建目录的目录名
    • @mode:指定该目录的访问权限,这些位将受到文件创建方式(umask)的修正
  • 返回值:成功返回0,失败返回 -1

15.1.2 删除目录

1 #include<unistd.h>
2 int rmdir(const char *pathname);
  • 函数功能:
    • 删除一个空目录
    • 使用 rmdir 函数时候,目录必须为空或该目录的链接计数为2(只包含 . 和 ..),且无其他进程打开此目录,否则调用失败
  • 返回值:
    • 成功返回 0,失败返回 -1   

15.1.3 打开目录

1 #include <sys/types.h>
2 #include <dirent.h>
3 DIR *opendir(const char *pathname);
  • 函数功能:打开目录
  • 返回值:成功返回目录指针,出错返回  NULL

15.1.4 读取目录

1 #include <sys/types.h>
2 #include <dirent.h>
3 struct dirent *readdir(DIR *dp);
  • 函数功能:读取目录
  • 返回值:成功返回指针,若在目录结尾或者出错,返回 NULL

15.1.5 重定位目录

1 #include <sys/types.h>
2 #include <dirent.h>
3 void rewinddir(dir(DIR *dp));
  • 函数功能:重新定位从头开始读取
  • 返回值:成功返回 0,出错返回 -1

15.1.6 关闭目录

1 #include <sys/types.h>
2 #include <dirent.h>
3 int closedir(DIR *dp)
  • 函数功能:关闭目录
  • 返回值:成功返回 0,出错返回 -1

15.1.7 dirent 结构体

1 struct dirent {
2     ino_t d_ino;       /* i-node number */
3     char  d_name[NAME_MAX + 1];   /* null - terminated filename */      
4 }

15.1.8 指定新的当前工作目录

1 #include<unistd.h>
2 int chdir(const char *pathname);
3 int fchdir(int fd);
  • 函数功能:分别用 pathname 或 fd 来指定新的当前工作目录
  • 返回值:成功返回0,出错返回 -1
  • 函数说明:当前工作目录是一个进程的属性,所以它只影响调用 chdir 的进程本身,而不影响其他进程

15.1.9 获得当前工作目录的绝对路径名

1 #include<unistd.h>
2 char *getcwd(char *buf, size_t size);
  • 函数功能:获得当前工作目录的绝对路径名,getcwd()会将当前的工作目录绝对路径复制到参数buf所指的内存空间
  • 返回值:成功返回  buff,出错返回 NULL  

15.2 例子

15.2.1 读取一个目录

  dir_read.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <dirent.h>
 4 #include <string.h>
 5 #include <sys/stat.h>
 6 #include <errno.h>
 7 
 8 int main(int argc, char *argv[])
 9 {
10     if(argc < 2) {
11         fprintf(stderr, "usage: %s dir\n", argv[0]);
12         exit(1);
13     }
14 
15     struct stat buff;
16     if(lstat(argv[1], &buff) < 0) {
17         perror("lstat error");
18         exit(1);
19     }
20 
21     //判断是否是目录
22     if(!S_ISDIR(buff.st_mode)) {
23         fprintf(stderr, "%s is not a directory\n", argv[1]);
24         exit(1);
25     }
26 
27     // 打开目录
28     DIR *dir = opendir(argv[1]);
29     if(dir == NULL) {
30         fprintf(stderr, "%s open error\n", argv[1]);
31         exit(1);
32     }
33 
34     //读取目录内的信息
35     struct dirent *ent;
36      while((ent = readdir(dir)) != NULL) {
37          printf("%-20s %10ld\n", ent->d_name, ent->d_ino);
38      }
39 
40      closedir(dir);
41 }

 编译运行: 

  

15.2.2 切换目录

  mchdir.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <fcntl.h>
 5 #include <string.h>
 6 #include <memory.h>
 7 
 8 int main(int argc, char *argv[])
 9 {
10     if(argc < 2) {
11         fprintf(stderr, "usage: %s\n", argv[0]);
12         exit(1);
13     }
14 
15     char buff[4096];
16     memset(buff, 0, sizeof(buff));
17     //获得当前工作目录
18     if(getcwd(buff, sizeof(buff)) != NULL) {
19         printf("current dir: %s\n", buff);
20     }
21 
22     //切换目录
23     if(chdir(argv[1]) < 0) {
24         perror("chdir error");
25         exit(1);
26     }
27 
28     memset(buff, 0, sizeof(buff));
29     if(getcwd(buff, sizeof(buff)) != NULL) {
30         printf("current dir: %s\n", buff);
31     }
32 
33     return 0;
34 }

  编译运行:

  

猜你喜欢

转载自www.cnblogs.com/kele-dad/p/9069729.html
今日推荐