readwrite.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * File : readwrite.c
  3. * This file is part of RT-TestCase in RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-02-10 Bernard first version
  13. */
  14. #include <rtthread.h>
  15. #include <dfs_posix.h>
  16. #define TEST_FN "/test.dat"
  17. static char test_data[120], buffer[120];
  18. /* 文件读写测试 */
  19. void readwrite(const char* filename)
  20. {
  21. int fd;
  22. int index, length;
  23. /* 只写 & 创建 打开 */
  24. fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
  25. if (fd < 0)
  26. {
  27. rt_kprintf("open file for write failed\n");
  28. return;
  29. }
  30. /* 准备写入数据 */
  31. for (index = 0; index < sizeof(test_data); index ++)
  32. {
  33. test_data[index] = index + 27;
  34. }
  35. /* 写入数据 */
  36. length = write(fd, test_data, sizeof(test_data));
  37. if (length != sizeof(test_data))
  38. {
  39. rt_kprintf("write data failed\n");
  40. close(fd);
  41. return;
  42. }
  43. /* 关闭文件 */
  44. close(fd);
  45. /* 只写并在末尾添加打开 */
  46. fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
  47. if (fd < 0)
  48. {
  49. rt_kprintf("open file for append write failed\n");
  50. return;
  51. }
  52. length = write(fd, test_data, sizeof(test_data));
  53. if (length != sizeof(test_data))
  54. {
  55. rt_kprintf("append write data failed\n");
  56. close(fd);
  57. return;
  58. }
  59. /* 关闭文件 */
  60. close(fd);
  61. /* 只读打开进行数据校验 */
  62. fd = open(TEST_FN, O_RDONLY, 0);
  63. if (fd < 0)
  64. {
  65. rt_kprintf("check: open file for read failed\n");
  66. return;
  67. }
  68. length = read(fd, buffer, sizeof(buffer));
  69. if (length != sizeof(buffer))
  70. {
  71. rt_kprintf("check: read file failed\n");
  72. close(fd);
  73. return;
  74. }
  75. for (index = 0; index < sizeof(test_data); index ++)
  76. {
  77. if (test_data[index] != buffer[index])
  78. {
  79. rt_kprintf("check: check data failed at %d\n", index);
  80. close(fd);
  81. return;
  82. }
  83. }
  84. length = read(fd, buffer, sizeof(buffer));
  85. if (length != sizeof(buffer))
  86. {
  87. rt_kprintf("check: read file failed\n");
  88. close(fd);
  89. return;
  90. }
  91. for (index = 0; index < sizeof(test_data); index ++)
  92. {
  93. if (test_data[index] != buffer[index])
  94. {
  95. rt_kprintf("check: check data failed at %d\n", index);
  96. close(fd);
  97. return;
  98. }
  99. }
  100. /* 检查数据完毕,关闭文件 */
  101. close(fd);
  102. /* 打印结果 */
  103. rt_kprintf("read/write done.\n");
  104. }
  105. #ifdef RT_USING_FINSH
  106. #include <finsh.h>
  107. FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
  108. #endif