Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
75 lines (73 loc) · 1.35 KB
/
Copy pathmain.c
File metadata and controls
75 lines (73 loc) · 1.35 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
67
68
69
70
71
72
73
74
75
#include "../include/fd.h"
int main()
{
char *path = "file.txt";
int fd;
char buf[40], buf2[] = "hello world";
int n, i;
if ((fd = open(path, O_RDWR)) < 0)
{
perror("open file failed");
return 1;
}
else
{
printf("open file successfully\n");
}
if ((n = read(fd, buf, 20)) < 0)
{
perror("read failed");
return 1;
}
else
{
printf("output read data:\n");
printf("%s\n", buf);
}
if ((i = lseek(fd, 11, SEEK_SET)) < 0)
{
perror("lseek error");
return 1;
}
else
{
if (write(fd, buf2, 11) < 0)
{
perror("write error");
return 1;
}
else
{
printf("write successfully\n");
}
}
close(fd);
if ((fd = open(path, O_RDWR)) < 0)
{
perror("open file failed");
return 1;
}
else
{
if ((n = read(fd, buf, 40) < 0))
{
perror("open file 2 failed");
return 1;
}
else
{
printf("read the changed data:\n");
printf("%s\n", buf);
}
if (close(fd) < 0)
{
perror("close file failed");
return 1;
}
else
{
printf("goodbye\n");
}
}
return 0;
}