-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreadwrite_sample.c
51 lines (46 loc) · 1.24 KB
/
readwrite_sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*
*/
/*
* 代码清单:文件读写例子
*
* 这个例子演示了如何读写一个文件。
*/
#include <rtthread.h>
#if RT_VER_NUM >= 0x40100
#include <fcntl.h> /* 当需要使用文件操作时,需要包含这个头文件 */
#else
#include <dfs_posix.h>
#endif /*RT_VER_NUM >= 0x40100*/
static void readwrite_sample(void)
{
int fd, size;
char s[] = "RT-Thread Programmer!", buffer[80];
rt_kprintf("Write string %s to test.txt.\n", s);
/* 以创建和读写模式打开 /text.txt 文件,如果该文件不存在则创建该文件*/
fd = open("/text.txt", O_WRONLY | O_CREAT);
if (fd >= 0)
{
write(fd, s, sizeof(s));
close(fd);
rt_kprintf("Write done.\n");
}
/* 以只读模式打开 /text.txt 文件 */
fd = open("/text.txt", O_RDONLY);
if (fd >= 0)
{
size = read(fd, buffer, sizeof(buffer));
close(fd);
rt_kprintf("Read from file test.txt : %s \n", buffer);
if (size < 0)
return ;
}
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(readwrite_sample, readwrite sample);