提交 3c831420 编写于 作者: B bernard.xiong

update file example.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@408 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 2f3882c7
......@@ -14,6 +14,7 @@
#include <rtthread.h>
#include <dfs_posix.h>
static char fullpath[256];
void list_dir(const char* path)
{
DIR *dir;
......
......@@ -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;
......
......@@ -14,18 +14,105 @@
#include <rtthread.h>
#include <dfs_posix.h>
#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;
}
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;
}
}
/* 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;
}
}
/* 检查数据完毕,关闭文件 */
close(fd);
/* 打印结果 */
rt_kprintf("read/write done.\n");
}
#ifdef RT_USING_FINSH
......
......@@ -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 */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册