-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathio.c
More file actions
66 lines (53 loc) · 1.31 KB
/
io.c
File metadata and controls
66 lines (53 loc) · 1.31 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* LittleFs file I/O test. Create, write, close, open, read, seek, close. */
int main(int ac, char* av[]) {
int rc = -1;
int fin = 0, fout = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC);
if (fout < 0) {
printf("error opening test.txt\n");
goto exit;
}
printf("file created\n");
char* s = "part 1 part 2";
if (write(fout, s, strlen(s)) != strlen(s)) {
printf("error writing test.txt\n");
goto exit_close;
}
printf("file written\n");
close(fout);
fout = 0;
printf("file closed\n");
rename("test.txt", "test2.txt");
printf("file renamed\n");
fin = open("test2.txt", O_RDONLY);
if (fin < 0) {
printf("error opening test.txt\n");
goto exit;
}
printf("file opened\n");
lseek(fin, 7, 0);
printf("file seeked\n");
char buf[32];
int l = read(fin, buf, sizeof(buf));
if (l < 0) {
printf("error reading test.txt\n");
goto exit_close;
}
buf[l] = 0;
printf("read: %s\n", buf);
if (strcmp(buf, "part 2")) {
printf("expected part2!");
goto exit_close;
}
close(fin);
fin = 0;
printf("file closed\n");
remove("test2.txt");
rc = 0;
exit_close:
if (fout)
close(fout);
if (fin)
close(fin);
exit:
return rc;
}