MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / readwrite

Function readwrite

examples/file/readwrite.c:22–147  ·  view source on GitHub ↗

file read write test */

Source from the content-addressed store, hash-verified

20
21/* file read write test */
22void readwrite(const char* filename)
23{
24 int fd;
25 int index, length;
26 char* test_data;
27 char* buffer;
28 int block_size = TEST_DATA_LEN;
29
30 /* open with write only & create */
31 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
32 if (fd < 0)
33 {
34 rt_kprintf("open file for write failed\n");
35 return;
36 }
37
38 test_data = rt_malloc(block_size);
39 if (test_data == RT_NULL)
40 {
41 rt_kprintf("no memory\n");
42 close(fd);
43 return;
44 }
45
46 buffer = rt_malloc(block_size);
47 if (buffer == RT_NULL)
48 {
49 rt_kprintf("no memory\n");
50 close(fd);
51 rt_free(test_data);
52 return;
53 }
54
55 /* prepare some data */
56 for (index = 0; index < block_size; index ++)
57 {
58 test_data[index] = index + 27;
59 }
60
61 /* write to file */
62 length = write(fd, test_data, block_size);
63 if (length != block_size)
64 {
65 rt_kprintf("write data failed\n");
66 close(fd);
67 goto __exit;
68 }
69
70 /* close file */
71 close(fd);
72
73 /* reopen the file with append to the end */
74 fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0);
75 if (fd < 0)
76 {
77 rt_kprintf("open file for append write failed\n");
78 goto __exit;;
79 }

Callers 1

cmd_readwriteFunction · 0.85

Calls 7

rt_kprintfFunction · 0.85
rt_mallocFunction · 0.85
rt_freeFunction · 0.85
openFunction · 0.50
closeFunction · 0.50
writeFunction · 0.50
readFunction · 0.50

Tested by

no test coverage detected