-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.c
45 lines (40 loc) · 988 Bytes
/
test4.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
//imprimir las 3 primeras letras
#include "get_next_line.h"
char *get_next_line(int fd)
{
char *saved;
char buffer[BUFFER_SIZE + 1];
int chars_read;
int i;
chars_read = read(fd, buffer, BUFFER_SIZE);
buffer[chars_read] = '\0';
saved = malloc((BUFFER_SIZE) * sizeof(char));
i = 0;
while (buffer[i])
{
saved[i] = buffer[i];
i++;
}
saved[i] = '\0';
return (saved);
}
int main(void)
{
int fd;
char *line;
fd = open("example.txt", O_RDONLY); //abrimos documento
if (fd == -1) //si no hay fd, error
{
write(2, "Can't open the file\n", 20);
return (1);
}
/*while ((line = get_next_line(fd)) != NULL) //siempre que la línea tenga algo y no sea nula, o sea no se termine.
{//esto hará que en cada while vayamos a una línea distinta. si lo declarámos arriba, tendríamos que poner abajo que vuelva a hacerlo cada vez que entra al while
printf("%s\n", line);
free(line);
}*/
line = get_next_line(fd);
printf("%s\n", line);
close (fd);
return (0);
}