stat_sample.c 1022 B

12345678910111213141516171819202122232425262728293031323334353637
  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. * 在函数中调用 stat() 函数
  15. * int stat(const char *file_name, struct stat *buf);
  16. * stat()函数用来将参数file_name 所指向的文件状态,
  17. * 复制到buf 指针所指的结构中(struct stat)。
  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 stat_sample(void)
  26. {
  27. int ret;
  28. struct stat buf;
  29. ret = stat("/text.txt", &buf);
  30. if (ret == 0)
  31. rt_kprintf("text.txt file size = %d\n", buf.st_size);
  32. else
  33. rt_kprintf("text.txt file not fonud\n");
  34. }
  35. /* 导出到 msh 命令列表中 */
  36. MSH_CMD_EXPORT(stat_sample, show text.txt stat sample);