opendir_sample.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #if RT_VER_NUM >= 0x40100
  21. #include <fcntl.h> /* 当需要使用文件操作时,需要包含这个头文件 */
  22. #else
  23. #include <dfs_posix.h>
  24. #endif /*RT_VER_NUM >= 0x40100*/
  25. static void opendir_sample(void)
  26. {
  27. DIR *dirp;
  28. /* 打开/dir_test 目录*/
  29. dirp = opendir("/dir_test");
  30. if (dirp == RT_NULL)
  31. {
  32. rt_kprintf("open directory error!\n");
  33. }
  34. else
  35. {
  36. rt_kprintf("open directory OK!\n");
  37. /* 在这儿进行读取目录相关操作*/
  38. /* ...... */
  39. /* 关闭目录 */
  40. closedir(dirp);
  41. }
  42. }
  43. /* 导出到 msh 命令列表中 */
  44. MSH_CMD_EXPORT(opendir_sample, open dir sample);