From cd039a8b49add8238c4e721bdebcaabcccdd99b9 Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Wed, 24 Mar 2010 15:26:48 +0000 Subject: add more Chinese comments. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@536 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- examples/file/readwrite.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'examples/file') diff --git a/examples/file/readwrite.c b/examples/file/readwrite.c index 8dc29837ef..17d173c353 100644 --- a/examples/file/readwrite.c +++ b/examples/file/readwrite.c @@ -1,30 +1,24 @@ /* - * File : readwrite.c - * This file is part of RT-TestCase in RT-Thread RTOS - * COPYRIGHT (C) 2010, RT-Thread Development Team + * 代码清单:文件读写例子 * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE - * - * Change Logs: - * Date Author Notes - * 2010-02-10 Bernard first version + * 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。 */ + #include -#include +#include /* 当需要使用文件操作时,需要包含这个头文件 */ #define TEST_FN "/test.dat" +/* 测试用的数据和缓冲 */ static char test_data[120], buffer[120]; -/* Îļþ¶Áд²âÊÔ */ +/* 文件读写测试 */ void readwrite(const char* filename) { int fd; int index, length; - /* ֻд & ´´½¨ ´ò¿ª */ + /* 只写 & 创建 打开 */ fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0); if (fd < 0) { @@ -32,13 +26,13 @@ void readwrite(const char* filename) return; } - /* ׼±¸дÈëÊý¾Ý */ + /* 准备写入数据 */ for (index = 0; index < sizeof(test_data); index ++) { test_data[index] = index + 27; } - /* дÈëÊý¾Ý */ + /* 写入数据 */ length = write(fd, test_data, sizeof(test_data)); if (length != sizeof(test_data)) { @@ -47,10 +41,10 @@ void readwrite(const char* filename) return; } - /* ¹رÕÎļþ */ + /* 关闭文件 */ close(fd); - /* ֻд²¢ÔÚĩβÌí¼Ӵò¿ª */ + /* 只写并在末尾添加打开 */ fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0); if (fd < 0) { @@ -65,10 +59,10 @@ void readwrite(const char* filename) close(fd); return; } - /* ¹رÕÎļþ */ + /* 关闭文件 */ close(fd); - /* ֻ¶Á´򿪽øÐÐÊý¾ÝУÑé */ + /* 只读打开进行数据校验 */ fd = open(TEST_FN, O_RDONLY, 0); if (fd < 0) { @@ -76,6 +70,7 @@ void readwrite(const char* filename) return; } + /* 读取数据(应该为第一次写入的数据) */ length = read(fd, buffer, sizeof(buffer)); if (length != sizeof(buffer)) { @@ -83,6 +78,8 @@ void readwrite(const char* filename) close(fd); return; } + + /* 检查数据是否正确 */ for (index = 0; index < sizeof(test_data); index ++) { if (test_data[index] != buffer[index]) @@ -93,6 +90,7 @@ void readwrite(const char* filename) } } + /* 读取数据(应该为第二次写入的数据) */ length = read(fd, buffer, sizeof(buffer)); if (length != sizeof(buffer)) { @@ -100,6 +98,8 @@ void readwrite(const char* filename) close(fd); return; } + + /* 检查数据是否正确 */ for (index = 0; index < sizeof(test_data); index ++) { if (test_data[index] != buffer[index]) @@ -109,13 +109,15 @@ void readwrite(const char* filename) return; } } - /* ¼ì²éÊý¾ÝÍê±ϣ¬¹رÕÎļþ */ + + /* 检查数据完毕,关闭文件 */ close(fd); - /* ´òӡ½á¹û */ + /* 打印结果 */ rt_kprintf("read/write done.\n"); } #ifdef RT_USING_FINSH #include +/* 输出函数到finsh shell命令行中 */ FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test); #endif -- cgit v1.3.1