-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
83 lines (70 loc) · 2.13 KB
/
get_next_line.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
72
73
74
75
76
77
78
79
80
81
82
83
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: susajid <susajid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/15 12:46:09 by susajid #+# #+# */
/* Updated: 2023/11/27 11:15:21 by susajid ### ########.fr */
/* */
/* ************************************************************************** */
// #include <fcntl.h>
// #include <stdio.h>
#include "get_next_line.h"
char *get_next_line(int fd)
{
static char *remaining;
char *result;
char *temp;
int bytes;
bytes = find_line(fd, &remaining);
if (bytes < 0)
{
free(remaining);
remaining = NULL;
return (NULL);
}
result = ft_substr(remaining, 0, bytes);
if (!result)
{
free(remaining);
remaining = NULL;
return (NULL);
}
temp = remaining;
remaining = ft_substr(remaining, bytes, ft_strlen(remaining + bytes));
free(temp);
return (result);
}
// test.txt
// hello
// world
// line
// universe
// school
// int main(void)
// {
// int fd = open("test.txt", O_RDONLY);
// char *line = get_next_line(fd);
// printf("Test Case 1 Result: %s", line);
// free(line);
// line = get_next_line(fd);
// printf("Test Case 2 Result: %s", line);
// free(line);
// line = get_next_line(fd);
// printf("Test Case 3 Result: %s", line);
// free(line);
// line = get_next_line(fd);
// printf("Test Case 4 Result: %s", line);
// free(line);
// line = get_next_line(fd);
// printf("Test Case 5 Result: %s", line);
// free(line);
// line = get_next_line(fd);
// printf("Test Case 6 Result: %s", line);
// free(line);
// close(fd);
// printf("\n%s\n", get_next_line(fd));
// return (0);
// }