-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlseek_test.c
54 lines (44 loc) · 1.03 KB
/
lseek_test.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
52
53
54
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#define BUFFSZ 1000
void main()
{
int fd = open("hello", O_RDONLY);
char buf[BUFFSZ];
int ret;
if (fd < 0)
return;
memset(buf, 0, BUFFSZ);
read(fd, buf, 9);
ret = lseek(fd, 0, SEEK_CUR);
printf("1-> %d %s\n", ret, buf);
lseek(fd, 2, SEEK_SET);
memset(buf, 0, BUFFSZ);
read(fd, buf, 2);
ret = lseek(fd, 0, SEEK_CUR);
printf("2-> %d %s\n", ret, buf);
lseek(fd, 2, SEEK_CUR);
memset(buf, 0, BUFFSZ);
read(fd, buf, 3);
ret = lseek(fd, 0, SEEK_CUR);
printf("3-> %d %s\n", ret, buf);
ret = lseek(fd, -8, SEEK_END);
lseek(fd, ret, SEEK_SET);
memset(buf, 0, BUFFSZ);
read(fd, buf, 4);
ret = lseek(fd, 0, SEEK_CUR);
printf("4-> %d %s\n", ret, buf);
ret = lseek(fd, -1, SEEK_CUR);
lseek(fd, ret, SEEK_SET);
memset(buf, 0, BUFFSZ);
read(fd, buf, 4);
ret = lseek(fd, 0, SEEK_CUR);
printf("5-> %d %s\n", ret, buf);
ret = lseek(fd, 0, SEEK_SET);
memset(buf, 0, BUFFSZ);
read(fd, buf, 4);
ret = lseek(fd, 0, SEEK_CUR);
printf("6-> %d %s\n", ret, buf);
close(fd);
}