rtt-f030/examples/file/readwrite.c

122 lines
2.5 KiB
C
Raw Normal View History

/*
* File : readwrite.c
* This file is part of RT-TestCase in RT-Thread RTOS
* COPYRIGHT (C) 2010, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2010-02-10 Bernard first version
*/
#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;
/* ֻд & <20><><EFBFBD><EFBFBD> <20><><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;
}
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
#include <finsh.h>
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
#endif