-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
71 lines (63 loc) · 1.88 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-haib <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 17:17:15 by ael-haib #+# #+# */
/* Updated: 2024/07/11 17:17:19 by ael-haib ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
char buf[100];
int fd;
int nb_read;
int count;
int pos = 0;
static char var[100];
fd = open("cat.txt", O_RDWR | O_CREAT, 777);
if (fd == -1)
{
perror("failed");
return 1;
}
if (write(fd, "KKKKKKKK", 8) == -1)
{
perror("failed");
close(fd);
return 1;
}
lseek(fd, 0, SEEK_SET);
count = 0;
while ((nb_read = read(fd, buf, 2)) > 0)
{
buf[nb_read] = '\0';
if (pos + nb_read < sizeof(var))
{
memcpy(&var[pos], buf, nb_read);
pos += nb_read;
var[pos] = '\0';
}
else
{
fprintf(stderr, "static array is full\n");
close(fd);
return 1;
}
count++;
}
printf("%s\n", var);
if (nb_read == -1)
{
perror("read error");
close(fd);
return 1;
}
return 0;
}