-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathp6.c
41 lines (33 loc) · 746 Bytes
/
p6.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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
int fd;
if ((fd = open("test.txt", O_RDWR | O_APPEND)) == -1) {
printf("open failed\n");
exit(0);
}
write(fd, "World!\n", 7);
int r;
if ((r = lseek(fd, 0, SEEK_SET)) == -1) {
printf("lseek failed\n");
close(fd);
exit(0);
}
write(fd, "Hello", 5);
close(fd);
if ((fd = open("test.txt", O_RDWR | O_APPEND)) == -1) {
printf("open failed\n");
exit(0);
}
if ((r = lseek(fd, 0, SEEK_SET)) == -1) {
printf("lseek failed\n");
close(fd);
exit(0);
}
char str[32];
read(fd, str, 15);
printf("%s\n", str);
close(fd);
return 0;
}