|
1 | 1 | /* |
2 | | - * 代码清单:文件读写例子 |
| 2 | + * Copyright (c) 2006-2020, RT-Thread Development Team |
3 | 3 | * |
4 | | - * 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。 |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2010-02-10 Bernard first version |
| 9 | + * 2020-04-12 Jianjia Ma add msh cmd |
5 | 10 | */ |
6 | 11 |
|
7 | 12 | #include <rtthread.h> |
8 | | -#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */ |
9 | | - |
10 | | -#define TEST_FN "/test.dat" |
| 13 | +#include <dfs_posix.h> |
11 | 14 |
|
12 | | -/* 测试用的数据和缓冲 */ |
13 | | -static char test_data[120], buffer[120]; |
| 15 | +#define TEST_DATA_LEN 120 |
14 | 16 |
|
15 | | -/* 文件读写测试 */ |
| 17 | +/* file read write test */ |
16 | 18 | void readwrite(const char* filename) |
17 | 19 | { |
18 | 20 | int fd; |
19 | 21 | int index, length; |
| 22 | + char* test_data; |
| 23 | + char* buffer; |
| 24 | + int block_size = TEST_DATA_LEN; |
20 | 25 |
|
21 | | - /* 只写 & 创建 打开 */ |
22 | | - fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0); |
| 26 | + /* open with write only & create */ |
| 27 | + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0); |
23 | 28 | if (fd < 0) |
24 | 29 | { |
25 | 30 | rt_kprintf("open file for write failed\n"); |
26 | 31 | return; |
27 | 32 | } |
28 | 33 |
|
29 | | - /* 准备写入数据 */ |
30 | | - for (index = 0; index < sizeof(test_data); index ++) |
| 34 | + test_data = rt_malloc(block_size); |
| 35 | + if (test_data == RT_NULL) |
| 36 | + { |
| 37 | + rt_kprintf("no memory\n"); |
| 38 | + close(fd); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + buffer = rt_malloc(block_size); |
| 43 | + if (buffer == RT_NULL) |
| 44 | + { |
| 45 | + rt_kprintf("no memory\n"); |
| 46 | + close(fd); |
| 47 | + rt_free(test_data); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + /* prepare some data */ |
| 52 | + for (index = 0; index < block_size; index ++) |
31 | 53 | { |
32 | 54 | test_data[index] = index + 27; |
33 | 55 | } |
34 | 56 |
|
35 | | - /* 写入数据 */ |
36 | | - length = write(fd, test_data, sizeof(test_data)); |
37 | | - if (length != sizeof(test_data)) |
| 57 | + /* write to file */ |
| 58 | + length = write(fd, test_data, block_size); |
| 59 | + if (length != block_size) |
38 | 60 | { |
39 | 61 | rt_kprintf("write data failed\n"); |
40 | 62 | close(fd); |
41 | | - return; |
| 63 | + goto __exit; |
42 | 64 | } |
43 | 65 |
|
44 | | - /* 关闭文件 */ |
| 66 | + /* close file */ |
45 | 67 | close(fd); |
46 | 68 |
|
47 | | - /* 只写并在末尾添加打开 */ |
48 | | - fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0); |
| 69 | + /* reopen the file with append to the end */ |
| 70 | + fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0); |
49 | 71 | if (fd < 0) |
50 | 72 | { |
51 | 73 | rt_kprintf("open file for append write failed\n"); |
52 | | - return; |
| 74 | + goto __exit;; |
53 | 75 | } |
54 | 76 |
|
55 | | - length = write(fd, test_data, sizeof(test_data)); |
56 | | - if (length != sizeof(test_data)) |
| 77 | + length = write(fd, test_data, block_size); |
| 78 | + if (length != block_size) |
57 | 79 | { |
58 | 80 | rt_kprintf("append write data failed\n"); |
59 | 81 | close(fd); |
60 | | - return; |
| 82 | + goto __exit; |
61 | 83 | } |
62 | | - /* 关闭文件 */ |
| 84 | + /* close the file */ |
63 | 85 | close(fd); |
64 | 86 |
|
65 | | - /* 只读打开进行数据校验 */ |
66 | | - fd = open(TEST_FN, O_RDONLY, 0); |
| 87 | + /* open the file for data validation. */ |
| 88 | + fd = open(filename, O_RDONLY, 0); |
67 | 89 | if (fd < 0) |
68 | 90 | { |
69 | 91 | rt_kprintf("check: open file for read failed\n"); |
70 | | - return; |
| 92 | + goto __exit; |
71 | 93 | } |
72 | 94 |
|
73 | | - /* 读取数据(应该为第一次写入的数据) */ |
74 | | - length = read(fd, buffer, sizeof(buffer)); |
75 | | - if (length != sizeof(buffer)) |
| 95 | + /* read the data (should be the data written by the first time ) */ |
| 96 | + length = read(fd, buffer, block_size); |
| 97 | + if (length != block_size) |
76 | 98 | { |
77 | 99 | rt_kprintf("check: read file failed\n"); |
78 | 100 | close(fd); |
79 | | - return; |
| 101 | + goto __exit; |
80 | 102 | } |
81 | 103 |
|
82 | | - /* 检查数据是否正确 */ |
83 | | - for (index = 0; index < sizeof(test_data); index ++) |
| 104 | + /* validate */ |
| 105 | + for (index = 0; index < block_size; index ++) |
84 | 106 | { |
85 | 107 | if (test_data[index] != buffer[index]) |
86 | 108 | { |
87 | 109 | rt_kprintf("check: check data failed at %d\n", index); |
88 | 110 | close(fd); |
89 | | - return; |
| 111 | + goto __exit; |
90 | 112 | } |
91 | 113 | } |
92 | 114 |
|
93 | | - /* 读取数据(应该为第二次写入的数据) */ |
94 | | - length = read(fd, buffer, sizeof(buffer)); |
95 | | - if (length != sizeof(buffer)) |
| 115 | + /* read the data (should be the second time data) */ |
| 116 | + length = read(fd, buffer, block_size); |
| 117 | + if (length != block_size) |
96 | 118 | { |
97 | 119 | rt_kprintf("check: read file failed\n"); |
98 | 120 | close(fd); |
99 | | - return; |
| 121 | + goto __exit; |
100 | 122 | } |
101 | 123 |
|
102 | | - /* 检查数据是否正确 */ |
103 | | - for (index = 0; index < sizeof(test_data); index ++) |
| 124 | + /* validate */ |
| 125 | + for (index = 0; index < block_size; index ++) |
104 | 126 | { |
105 | 127 | if (test_data[index] != buffer[index]) |
106 | 128 | { |
107 | 129 | rt_kprintf("check: check data failed at %d\n", index); |
108 | 130 | close(fd); |
109 | | - return; |
| 131 | + goto __exit; |
110 | 132 | } |
111 | 133 | } |
112 | 134 |
|
113 | | - /* 检查数据完毕,关闭文件 */ |
| 135 | + /* close the file */ |
114 | 136 | close(fd); |
115 | | - /* 打印结果 */ |
116 | | - rt_kprintf("read/write done.\n"); |
| 137 | + /* print result */ |
| 138 | + rt_kprintf("read/write test successful!\n"); |
| 139 | + |
| 140 | +__exit: |
| 141 | + rt_free(test_data); |
| 142 | + rt_free(buffer); |
117 | 143 | } |
118 | 144 |
|
119 | 145 | #ifdef RT_USING_FINSH |
120 | 146 | #include <finsh.h> |
121 | | -/* 输出函数到finsh shell命令行中 */ |
| 147 | +/* export to finsh */ |
122 | 148 | FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test); |
123 | | -#endif |
| 149 | + |
| 150 | +#ifdef FINSH_USING_MSH |
| 151 | +static void cmd_readwrite(int argc, char *argv[]) |
| 152 | +{ |
| 153 | + char* filename; |
| 154 | + |
| 155 | + if(argc == 2) |
| 156 | + { |
| 157 | + filename = argv[1]; |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + rt_kprintf("Usage: readwrite [file_path]\n"); |
| 162 | + return; |
| 163 | + } |
| 164 | + readwrite(filename); |
| 165 | +} |
| 166 | +FINSH_FUNCTION_EXPORT_ALIAS(cmd_readwrite, __cmd_readwrite, perform file read and write test); |
| 167 | +#endif /* FINSH_USING_MSH */ |
| 168 | +#endif /* RT_USING_FINSH */ |
0 commit comments