opendir_sample.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. *
  9. */
  10. /*
  11. * 程序清单:打开目录
  12. *
  13. * 程序会创建一个操作文件的函数并导出到msh命令列表
  14. * 在函数中调用 opendir() 函数
  15. * DIR* opendir(const char* name);
  16. * opendir()函数用来打开一个目录,参数name 为目录路径名。
  17. * 若读取目录成功,返回该目录结构,若读取目录失败,返回RT_NULL。
  18. */
  19. #include <rtthread.h>
  20. #include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
  21. static void opendir_sample(void)
  22. {
  23. DIR *dirp;
  24. /* 打开/dir_test 目录*/
  25. dirp = opendir("/dir_test");
  26. if (dirp == RT_NULL)
  27. {
  28. rt_kprintf("open directory error!\n");
  29. }
  30. else
  31. {
  32. rt_kprintf("open directory OK!\n");
  33. /* 在这儿进行读取目录相关操作*/
  34. /* ...... */
  35. /* 关闭目录 */
  36. closedir(dirp);
  37. }
  38. }
  39. /* 导出到 msh 命令列表中 */
  40. MSH_CMD_EXPORT(opendir_sample, open dir sample);