《unix高级环境编程》文件和目录——目录操作

谁践踏了优雅 2022-08-13 14:57 268阅读 0赞

目录基本操作

目录的基本操作包括打开目录opendir、读取目录readdir、关闭目录closedir等操作,如下说明:

  1. /********************
  2. * 目录操作:
  3. * 1、打开目录opendir
  4. *
  5. * 函数功能:打开一个目录;
  6. * 返回值:若成功返回一个目录指针,若出错则返回NULL指针;
  7. * 函数原型:
  8. * #include <sys/types.h>
  9. * #include <dirent.h>
  10. * DIR *opendir(const char *pathname);
  11. *
  12. * 2、读一个目录readdir
  13. *
  14. * 函数功能:读取目录
  15. * 返回值:若成功返回指针,若在目录结尾或出错则返回NULL;
  16. * 函数原型:
  17. * #include <unistd.h>
  18. * #include <dirent.h>
  19. * struct dirent *readdir(DIR *dp);
  20. *
  21. * 3、重设与dp关联的目录中的位置
  22. *
  23. * 函数功能:重设与dp关联的在目录中的位置;
  24. * 没有返回值;
  25. * 函数原型:
  26. * #include <sys/types.h>
  27. * #include <dirent.h>
  28. * void rewinddir(DIR *dp);
  29. *
  30. * 4、dp关联的在目录的当前位置return current location in directory stream.
  31. *
  32. * 函数功能:获取与dp关联的在目录中的当前位置;
  33. * 返回值:若成功返回与dp关联的在目录中的当前位置,若出错返回-1;
  34. * 函数原型:
  35. * #include <dirent.h>
  36. * off_t telldir(DIR *dp);
  37. *
  38. * 5、The seekdir() function sets the location in the directory
  39. * stream from which the next readdir() call will start.
  40. * seekdir() should be used with an offset returned by telldir().
  41. *
  42. * 函数功能:设置下一个readdir调用在目录的位置
  43. * 无返回值
  44. * 函数原型:
  45. * #include <dirent.h>
  46. * void seekdir(DIR *dp, off_t offset);
  47. *
  48. * 6、关闭一个已打开的目录
  49. *
  50. * 函数功能:关闭一个已打开的目录
  51. * 返回值:若成功返回0,若出错返回-1;
  52. * 函数原型:
  53. * #include <sys/types.h>
  54. * #include <dirent.h>
  55. * int closedir(DIR *dp);
  56. * **********************************/

目录信息结构

  1. /************************************
  2. * struct dirent结构信息
  3. * *******/
  4. struct dirent
  5. {
  6. long d_ino; /* inode number */
  7. off_t d_off; /* offset to this dirent */
  8. unsigned short d_reclen; /* length of this d_name */
  9. char d_name [NAME_MAX+1]; /* file name (null-terminated) */
  10. };

目录创建、删除和更改工作目录

  1. /***********
  2. * 目录创建
  3. * 函数功能:创建一个空目录
  4. * 返回值:若成功则返回0,若出错则返回-1;
  5. * 函数原型:
  6. * #include <sys/stat.h>
  7. * int mkdir(const char *pathname, mode_t mode);
  8. * 说明:
  9. * 此函数创建一个新的空目录,其中.和..目录项是自动创建的。
  10. * 所指定的文件访问权限mode由进程的文件模式创建屏蔽字修改。
  11. *
  12. * 删除空目录
  13. * 函数功能:删除一个空的目录
  14. * 返回值:若成功则返回0,若出错则返回-1;
  15. * 函数原型:
  16. * #include <unistd.h>
  17. * int rmdir(const char *pathname);
  18. * 说明:
  19. * 如果调用该函数使目录的链接数为0,且没有其他进程打开该目录,则释放该目录空间。
  20. * 若链接数为0,此时有进程打开该目录,在进程关闭前不会释放目录空间。
  21. * ***************/
  22. /*************************
  23. * 更改当前工作目录。
  24. * 函数功能:更改当前的工作目录;
  25. * 返回值:若成功则返回0,若出错则返回-1;
  26. * 函数原型:
  27. * #include <unistd.h>
  28. * int chdir(const char *pathname);
  29. * int fchdir(int filedes);
  30. * 说明:
  31. * 这两个函数分别用pathname或打开文件描述符来指定当前的工作目录。
  32. * **********/
  33. /*************
  34. * 获取当前工作目录的绝对路径
  35. * 函数原型:
  36. * #include <unistd.h>
  37. * char *getcwd(char *buf, size_t size);
  38. * ***************/

参考资料

《unix高级环境编程》

发表评论

表情:
评论列表 (有 0 条评论,268人围观)

还没有评论,来说两句吧...

相关阅读