mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2025-12-29 10:30:24 +00:00
update file example.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@408 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
@@ -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];
|
||||
|
||||
/* <20>ļ<EFBFBD><C4BC><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD> */
|
||||
void readwrite(const char* filename)
|
||||
{
|
||||
int fd;
|
||||
int index, length;
|
||||
|
||||
/* ֻд<D6BB><D0B4><EFBFBD><EFBFBD> */
|
||||
/* ֻд & <20><><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("open file for write failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* <><D7BC>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
for (index = 0; index < sizeof(test_data); index ++)
|
||||
{
|
||||
test_data[index] = index + 27;
|
||||
}
|
||||
|
||||
/* д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
length = write(fd, test_data, sizeof(test_data));
|
||||
if (length != sizeof(test_data))
|
||||
{
|
||||
rt_kprintf("write data failed\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* <20>ر<EFBFBD><D8B1>ļ<EFBFBD> */
|
||||
close(fd);
|
||||
|
||||
/* ֻд<D6BB><D0B4><EFBFBD><EFBFBD>ĩβ<C4A9><CEB2><EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD> */
|
||||
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;
|
||||
}
|
||||
/* <20>ر<EFBFBD><D8B1>ļ<EFBFBD> */
|
||||
close(fd);
|
||||
|
||||
/* ֻ<><D6BB><EFBFBD><EFBFBD><F2BFAABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3> */
|
||||
fd = open(TEST_FN, O_RDONLY, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("check: open file for read failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* lseek<65><6B><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
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;
|
||||
}
|
||||
}
|
||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3>ر<EFBFBD><D8B1>ļ<EFBFBD> */
|
||||
close(fd);
|
||||
/* <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD> */
|
||||
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 */
|
||||
|
||||
Reference in New Issue
Block a user