2025-06-27 00:32:57 +08:00
|
|
|
|
/*
|
2025-07-05 19:47:28 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>嵥<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
*
|
2025-07-05 19:47:28 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ζ<EFBFBD>дһ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2025-06-27 00:32:57 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
2025-07-05 19:47:28 +08:00
|
|
|
|
#include <dfs_posix.h> /* <20><><EFBFBD><EFBFBD>Ҫʹ<D2AA><CAB9><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ļ<EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
|
|
|
|
|
|
#define TEST_FN "/test.dat"
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>ݺͻ<DDBA><CDBB><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
static char test_data[120], buffer[120];
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20>ļ<EFBFBD><C4BC><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
void readwrite(const char* filename)
|
|
|
|
|
|
{
|
|
|
|
|
|
int fd;
|
|
|
|
|
|
int index, length;
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* ֻд & <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
rt_kprintf("open file for write failed\n");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <><D7BC>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
for (index = 0; index < sizeof(test_data); index ++)
|
|
|
|
|
|
{
|
|
|
|
|
|
test_data[index] = index + 27;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
length = write(fd, test_data, sizeof(test_data));
|
|
|
|
|
|
if (length != sizeof(test_data))
|
|
|
|
|
|
{
|
|
|
|
|
|
rt_kprintf("write data failed\n");
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20>ر<EFBFBD><D8B1>ļ<EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
close(fd);
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* ֻд<D6BB><D0B4><EFBFBD><EFBFBD>ĩβ<C4A9><CEB2><EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20>ر<EFBFBD><D8B1>ļ<EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
close(fd);
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* ֻ<><D6BB><EFBFBD><EFBFBD><F2BFAABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
fd = open(TEST_FN, O_RDONLY, 0);
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
rt_kprintf("check: open file for read failed\n");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>(Ӧ<><D3A6>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>) */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
length = read(fd, buffer, sizeof(buffer));
|
|
|
|
|
|
if (length != sizeof(buffer))
|
|
|
|
|
|
{
|
|
|
|
|
|
rt_kprintf("check: read file failed\n");
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>(Ӧ<><D3A6>Ϊ<EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>) */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
length = read(fd, buffer, sizeof(buffer));
|
|
|
|
|
|
if (length != sizeof(buffer))
|
|
|
|
|
|
{
|
|
|
|
|
|
rt_kprintf("check: read file failed\n");
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3>ر<EFBFBD><D8B1>ļ<EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
close(fd);
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
rt_kprintf("read/write done.\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
|
|
#include <finsh.h>
|
2025-07-05 19:47:28 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>finsh shell<6C><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
2025-06-27 00:32:57 +08:00
|
|
|
|
FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
|
|
|
|
|
|
#endif
|