update file example.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@408 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong 2010-02-11 15:59:25 +00:00
parent 2f3882c714
commit 3c831420de
4 changed files with 111 additions and 7 deletions

View File

@ -14,6 +14,7 @@
#include <rtthread.h> #include <rtthread.h>
#include <dfs_posix.h> #include <dfs_posix.h>
static char fullpath[256];
void list_dir(const char* path) void list_dir(const char* path)
{ {
DIR *dir; DIR *dir;

View File

@ -22,7 +22,7 @@ void readspeed(const char* filename, int block_size)
rt_size_t total_length; rt_size_t total_length;
rt_tick_t tick; rt_tick_t tick;
fd = open(filename, 0, DFS_O_RDONLY); fd = open(filename, 0, O_RDONLY);
if (fd < 0) if (fd < 0)
{ {
rt_kprintf("open file:%s failed\n", filename); rt_kprintf("open file:%s failed\n", filename);
@ -45,7 +45,7 @@ void readspeed(const char* filename, int block_size)
int length; int length;
length = read(fd, buff_ptr, block_size); length = read(fd, buff_ptr, block_size);
if (length == 0) break; if (length <= 0) break;
total_length += length; total_length += length;
} }
tick = rt_tick_get() - tick; tick = rt_tick_get() - tick;

View File

@ -14,18 +14,105 @@
#include <rtthread.h> #include <rtthread.h>
#include <dfs_posix.h> #include <dfs_posix.h>
#define TEST_FN "/test.dat"
static char test_data[120], buffer[120];
/* 文件读写测试 */ /* 文件读写测试 */
void readwrite(const char* filename) void readwrite(const char* filename)
{ {
int fd; 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 #ifdef RT_USING_FINSH

View File

@ -16,11 +16,11 @@
void writespeed(const char* filename, int total_length, int block_size) void writespeed(const char* filename, int total_length, int block_size)
{ {
int fd; int fd, index, length;
char *buff_ptr; char *buff_ptr;
rt_tick_t tick; rt_tick_t tick;
fd = open(filename, 0, O_WRONLY); fd = open(filename, O_WRONLY | O_WRONLY | O_TRUNC, 0);
if (fd < 0) if (fd < 0)
{ {
rt_kprintf("open file:%s failed\n", filename); 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 */ /* prepare write data */
for (index = 0; index < block_size; index++)
{
buff_ptr[index] = index;
}
index = 0;
/* get the beginning tick */
tick = rt_tick_get(); 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; tick = rt_tick_get() - tick;
/* close file and release memory */ /* close file and release memory */