diff options
| author | majianjia <[email protected]> | 2020-04-12 23:56:43 +0100 |
|---|---|---|
| committer | majianjia <[email protected]> | 2020-04-12 23:56:43 +0100 |
| commit | 20146636602204c2bf5084a62f22678aa9976f19 (patch) | |
| tree | 8a9be616ba9ac7ab350b6618b29a5bc87571ad4d /examples/file/readwrite.c | |
| parent | 5b300113f7fae2042bd82da31c469f916ed01a22 (diff) | |
Export file examples to MSH
also:
- change static buffer to dynamiclly allocated buffer.
- replace Chinese comment to English comment in some files.
- add apache-2 license
Diffstat (limited to 'examples/file/readwrite.c')
| -rw-r--r-- | examples/file/readwrite.c | 137 |
1 files changed, 91 insertions, 46 deletions
diff --git a/examples/file/readwrite.c b/examples/file/readwrite.c index 6f25249c61..5738323805 100644 --- a/examples/file/readwrite.c +++ b/examples/file/readwrite.c @@ -1,123 +1,168 @@ /* - * 代码清单:文件读写例子 + * Copyright (c) 2006-2020, RT-Thread Development Team * - * 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。 + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2010-02-10 Bernard first version + * 2020-04-12 Jianjia Ma add msh cmd */ #include <rtthread.h> -#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */ - -#define TEST_FN "/test.dat" +#include <dfs_posix.h> -/* 测试用的数据和缓冲 */ -static char test_data[120], buffer[120]; +#define TEST_DATA_LEN 120 -/* 文件读写测试 */ +/* file read write test */ void readwrite(const char* filename) { int fd; int index, length; + char* test_data; + char* buffer; + int block_size = TEST_DATA_LEN; - /* 只写 & 创建 打开 */ - fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0); + /* open with write only & create */ + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0); if (fd < 0) { rt_kprintf("open file for write failed\n"); return; } - /* 准备写入数据 */ - for (index = 0; index < sizeof(test_data); index ++) + test_data = rt_malloc(block_size); + if (test_data == RT_NULL) + { + rt_kprintf("no memory\n"); + close(fd); + return; + } + + buffer = rt_malloc(block_size); + if (buffer == RT_NULL) + { + rt_kprintf("no memory\n"); + close(fd); + rt_free(test_data); + return; + } + + /* prepare some data */ + for (index = 0; index < block_size; index ++) { test_data[index] = index + 27; } - /* 写入数据 */ - length = write(fd, test_data, sizeof(test_data)); - if (length != sizeof(test_data)) + /* write to file */ + length = write(fd, test_data, block_size); + if (length != block_size) { rt_kprintf("write data failed\n"); close(fd); - return; + goto __exit; } - /* 关闭文件 */ + /* close file */ close(fd); - /* 只写并在末尾添加打开 */ - fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0); + /* reopen the file with append to the end */ + fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0); if (fd < 0) { rt_kprintf("open file for append write failed\n"); - return; + goto __exit;; } - length = write(fd, test_data, sizeof(test_data)); - if (length != sizeof(test_data)) + length = write(fd, test_data, block_size); + if (length != block_size) { rt_kprintf("append write data failed\n"); close(fd); - return; + goto __exit; } - /* 关闭文件 */ + /* close the file */ close(fd); - /* 只读打开进行数据校验 */ - fd = open(TEST_FN, O_RDONLY, 0); + /* open the file for data validation. */ + fd = open(filename, O_RDONLY, 0); if (fd < 0) { rt_kprintf("check: open file for read failed\n"); - return; + goto __exit; } - /* 读取数据(应该为第一次写入的数据) */ - length = read(fd, buffer, sizeof(buffer)); - if (length != sizeof(buffer)) + /* read the data (should be the data written by the first time ) */ + length = read(fd, buffer, block_size); + if (length != block_size) { rt_kprintf("check: read file failed\n"); close(fd); - return; + goto __exit; } - /* 检查数据是否正确 */ - for (index = 0; index < sizeof(test_data); index ++) + /* validate */ + for (index = 0; index < block_size; index ++) { if (test_data[index] != buffer[index]) { rt_kprintf("check: check data failed at %d\n", index); close(fd); - return; + goto __exit; } } - /* 读取数据(应该为第二次写入的数据) */ - length = read(fd, buffer, sizeof(buffer)); - if (length != sizeof(buffer)) + /* read the data (should be the second time data) */ + length = read(fd, buffer, block_size); + if (length != block_size) { rt_kprintf("check: read file failed\n"); close(fd); - return; + goto __exit; } - /* 检查数据是否正确 */ - for (index = 0; index < sizeof(test_data); index ++) + /* validate */ + for (index = 0; index < block_size; index ++) { if (test_data[index] != buffer[index]) { rt_kprintf("check: check data failed at %d\n", index); close(fd); - return; + goto __exit; } } - /* 检查数据完毕,关闭文件 */ + /* close the file */ close(fd); - /* 打印结果 */ - rt_kprintf("read/write done.\n"); + /* print result */ + rt_kprintf("read/write test successful!\n"); + +__exit: + rt_free(test_data); + rt_free(buffer); } #ifdef RT_USING_FINSH #include <finsh.h> -/* 输出函数到finsh shell命令行中 */ +/* export to finsh */ FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test); -#endif + +#ifdef FINSH_USING_MSH +static void cmd_readwrite(int argc, char *argv[]) +{ + char* filename; + + if(argc == 2) + { + filename = argv[1]; + } + else + { + rt_kprintf("Usage: readwrite [file_path]\n"); + return; + } + readwrite(filename); +} +FINSH_FUNCTION_EXPORT_ALIAS(cmd_readwrite, __cmd_readwrite, perform file read and write test); +#endif /* FINSH_USING_MSH */ +#endif /* RT_USING_FINSH */ |
