فهرست منبع

Merge pull request #3 from RT-Thread-packages-by-SummerGift/2018_09_07

 【完善】:文件系统读写例程
yqiu 7 سال پیش
والد
کامیت
1f8ec537f9
2فایلهای تغییر یافته به همراه23 افزوده شده و 148 حذف شده
  1. 0 47
      openfile/openfile_sample.c
  2. 23 101
      readwrite/readwrite_sample.c

+ 0 - 47
openfile/openfile_sample.c

@@ -1,47 +0,0 @@
-/* 
- * Copyright (c) 2006-2018, RT-Thread Development Team 
- * 
- * SPDX-License-Identifier: Apache-2.0 
- * 
- * Change Logs: 
- * Date             Author      Notes
- * 
- */
-/*
- * 程序清单:打开文件
- *
- * 程序会创建一个操作文件的函数并导出到msh命令列表
- * 在函数中调用 open() close() 函数
- * int open(const char *pathname, int oflag, int mode); 打开或创建一个文件
- * int close(int fd);关闭文件
- *
-*/
-#include <rtthread.h>
-#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
-
-static void openfile_sample(void *parameter)
-{
-    int fd, size;
-    char s[] = "RT-Thread Programmer!\n", buffer[80];
-
-    /* 打开/text.txt 作写入,如果该文件不存在则建立该文件*/
-    fd = open("/text.txt", O_WRONLY | O_CREAT);
-    if (fd >= 0)
-    {
-        write(fd, s, sizeof(s));
-        close(fd);
-    }
-
-    /* 打开/text.txt 准备作读取动作*/
-    fd = open("/text.txt", O_RDONLY);
-    if (fd >= 0)
-    {
-        size = read(fd, buffer, sizeof(buffer));
-        close(fd);
-        if (size < 0)
-            return ;
-    }
-    rt_kprintf("%s", buffer);
-}
-/* 导出到 msh 命令列表中 */
-MSH_CMD_EXPORT(openfile_sample, open file sample);

+ 23 - 101
readwrite/readwrite_sample.c

@@ -1,127 +1,49 @@
-/* 
- * Copyright (c) 2006-2018, RT-Thread Development Team 
- * 
- * SPDX-License-Identifier: Apache-2.0 
- * 
- * Change Logs: 
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
  * Date             Author      Notes
- * 
+ *
  */
 /*
  * 代码清单:文件读写例子
  *
- * 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作
+ * 这个例子演示了如何读写一个文件。
  */
 
 #include <rtthread.h>
 #include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */
 
-#define TEST_FN     "/test.dat"
-
-/* 测试用的数据和缓冲 */
-static char test_data[120], buffer[120];
-
 static void readwrite_sample(void)
 {
-    int fd;
-    int index, length;
-
-    /* 只写 & 创建 打开 */
-    fd = open(TEST_FN, 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[index] = index + 48;
-    }
-
-    /* 写入数据 */
-    length = write(fd, test_data, sizeof(test_data));
-    if (length != sizeof(test_data))
-    {
-        rt_kprintf("write data failed\n");
-        close(fd);
-        return;
-    }
-
-    /* 关闭文件 */
-    close(fd);
-
-    /* 只写并在末尾添加打开 */
-    fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0);
-    if (fd < 0)
-    {
-        rt_kprintf("open file for append write failed\n");
-        return;
-    }
+    int fd, size;
+    char s[] = "RT-Thread Programmer!", buffer[80];
 
-    length = write(fd, test_data, sizeof(test_data));
-    if (length != sizeof(test_data))
-    {
-        rt_kprintf("append write data failed\n");
-        close(fd);          
-        return;
-    }
-    /* 关闭文件 */
-    close(fd);
+    rt_kprintf("Write string %s to test.txt.\n", s);
 
-    /* 只读打开进行数据校验 */
-    fd = open(TEST_FN, O_RDONLY, 0);
-    if (fd < 0)
+    /* 以创建和读写模式打开 /text.txt 文件,如果该文件不存在则创建该文件*/
+    fd = open("/text.txt", O_WRONLY | O_CREAT);
+    if (fd >= 0)
     {
-        rt_kprintf("check: open file for read failed\n");
-        return;
-    }
-
-    /* 读取数据(应该为第一次写入的数据) */
-    length = read(fd, buffer, sizeof(buffer));
-    if (length != sizeof(buffer))
-    {
-        rt_kprintf("check: read file failed\n");
+        write(fd, s, sizeof(s));
         close(fd);
-        return;
     }
 
-    /* 检查数据是否正确 */
-    for (index = 0; index < sizeof(test_data); index ++)
-    {
-        if (test_data[index] != buffer[index])
-        {
-            rt_kprintf("check: check data failed at %d\n", index);
-            close(fd);
-            return;
-        }
-    }
+    rt_kprintf("Write done.\n");
 
-    /* 读取数据(应该为第二次写入的数据) */
-    length = read(fd, buffer, sizeof(buffer));
-    if (length != sizeof(buffer))
+    /* 以只读模式打开 /text.txt 文件 */
+    fd = open("/text.txt", O_RDONLY);
+    if (fd >= 0)
     {
-        rt_kprintf("check: read file failed\n");
+        size = read(fd, buffer, sizeof(buffer));
         close(fd);
-        return;
-    }
-
-    /* 检查数据是否正确 */
-    for (index = 0; index < sizeof(test_data); index ++)
-    {
-        if (test_data[index] != buffer[index])
-        {
-            rt_kprintf("check: check data failed at %d\n", index);
-            close(fd);
-            return;
-        }
+        if (size < 0)
+            return ;
     }
 
-    /* 检查数据完毕,关闭文件 */
-    close(fd);
-    /* 打印结果 */
-    rt_kprintf("read/write done.\n");
+    rt_kprintf("Read from file test.txt : %s \n", buffer);
 }
 /* 导出到 msh 命令列表中 */
 MSH_CMD_EXPORT(readwrite_sample, readwrite sample);