diff --git a/examples/file/listdir.c b/examples/file/listdir.c index 86279163e5..76cf2f2e26 100644 --- a/examples/file/listdir.c +++ b/examples/file/listdir.c @@ -14,6 +14,7 @@ #include #include +static char fullpath[256]; void list_dir(const char* path) { DIR *dir; diff --git a/examples/file/readspeed.c b/examples/file/readspeed.c index 5d22756365..2426756fab 100644 --- a/examples/file/readspeed.c +++ b/examples/file/readspeed.c @@ -22,7 +22,7 @@ void readspeed(const char* filename, int block_size) rt_size_t total_length; rt_tick_t tick; - fd = open(filename, 0, DFS_O_RDONLY); + fd = open(filename, 0, O_RDONLY); if (fd < 0) { rt_kprintf("open file:%s failed\n", filename); @@ -45,7 +45,7 @@ void readspeed(const char* filename, int block_size) int length; length = read(fd, buff_ptr, block_size); - if (length == 0) break; + if (length <= 0) break; total_length += length; } tick = rt_tick_get() - tick; diff --git a/examples/file/readwrite.c b/examples/file/readwrite.c index b4086cf056..8dc29837ef 100644 --- a/examples/file/readwrite.c +++ b/examples/file/readwrite.c @@ -14,18 +14,105 @@ #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) + { + rt_kprintf("open file for write failed\n"); + 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)) + { + 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; + } + + 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); /* 只读打开进行数据校验 */ + fd = open(TEST_FN, O_RDONLY, 0); + if (fd < 0) + { + rt_kprintf("check: open file for read failed\n"); + return; + } - /* lseek测试,读出并校验数据 */ + length = read(fd, buffer, sizeof(buffer)); + if (length != sizeof(buffer)) + { + rt_kprintf("check: read file failed\n"); + 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; + } + } + + length = read(fd, buffer, sizeof(buffer)); + if (length != sizeof(buffer)) + { + rt_kprintf("check: read file failed\n"); + 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; + } + } + /* 检查数据完毕,关闭文件 */ + close(fd); + /* 打印结果 */ + rt_kprintf("read/write done.\n"); } #ifdef RT_USING_FINSH diff --git a/examples/file/writespeed.c b/examples/file/writespeed.c index d829d9977b..22b6d565d2 100644 --- a/examples/file/writespeed.c +++ b/examples/file/writespeed.c @@ -16,11 +16,11 @@ void writespeed(const char* filename, int total_length, int block_size) { - int fd; + int fd, index, length; char *buff_ptr; rt_tick_t tick; - fd = open(filename, 0, O_WRONLY); + fd = open(filename, O_WRONLY | O_WRONLY | O_TRUNC, 0); if (fd < 0) { rt_kprintf("open file:%s failed\n", filename); @@ -37,9 +37,25 @@ void writespeed(const char* filename, int total_length, int block_size) } /* prepare write data */ + for (index = 0; index < block_size; index++) + { + buff_ptr[index] = index; + } + index = 0; + /* get the beginning tick */ tick = rt_tick_get(); - + while (index < total_length / block_size) + { + length = write(fd, buff_ptr, block_size); + if (length != block_size) + { + rt_kprintf("write failed\n"); + break; + } + + index ++; + } tick = rt_tick_get() - tick; /* close file and release memory */