readme
This project has been created as part of the 42 curriculum by mleitao.
Description
get_next_line is a project that implements a function that reads and returns a line from a file descriptor. It uses a static variable to preserve leftover data between calls, allowing successive calls to return successive lines. The bonus part extends this functionality to support multiple file descriptors.
Overview
This function reads data from a file descriptor in fixed-size chunks into a buffer of size BUFFER_SIZE. Since a line may not be fully obtained in a single read, the data is stored in a static variable, allowing the function to retain and reuse it across multiple calls.
This function continues reading and appending data until it encounters a new line character, or reaches the end of the file. If more than one line is read at once, the extra characters are preserved in the static variable and used in subsequent calls.
Instructions
Requirements
- A C compiler, ideally
clang.
Usage
-
Clone the repository:
git clone ssh://git@codeberg.org/6times7/get_next_line.git -
Include
get_next_line.hin yourmain.c:#include "get_next_line.h" #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { int fd; char *line; fd = open("get_next_line.c", O_RDONLY); while ((line = get_next_line(fd)) != NULL) { printf("%s", line); free(line); } close(fd); return (0); } -
Compile it:
cc -Wall -Wextra -Werror get_next_line.c get_next_line_utils.c main.c
Resources
References
man 2 read- Peer discussions
- Using ssize_t vs int
- How do I find the maximum stack size?
LLM Usage
LLMs were used for:
- General guidance during project development, primarily to make code more idiomatic
- Parts of this README
Tests
This project was tested against gnlTester. Kudos to @Tripouille for providing this test suite.
1*This project has been created as part of the 42 curriculum by mleitao.*
2
3<h1 id="description">Description</h1>
4
5**get_next_line** is a project that implements a function that reads and returns a line from a file descriptor. It uses a static variable to preserve leftover data between calls, allowing successive calls to return successive lines. The bonus part extends this functionality to support multiple file descriptors.
6
7<h3 id="overview">Overview</h3>
8
9This function reads data from a file descriptor in fixed-size chunks into a buffer of size `BUFFER_SIZE`. Since a line may not be fully obtained in a single read, the data is stored in a static variable, allowing the function to retain and reuse it across multiple calls.
10
11This function continues reading and appending data until it encounters a new line character, or reaches the end of the file. If more than one line is read at once, the extra characters are preserved in the static variable and used in subsequent calls.
12
13<h1 id="instructions">Instructions</h1>
14
15<h3 id="requirements">Requirements</h3>
16
17- A C compiler, ideally `clang`.
18
19<h3 id="usage">Usage</h3>
20
211. Clone the repository:
22 ```bash
23 git clone ssh://git@codeberg.org/6times7/get_next_line.git
24 ```
25
262. Include [`get_next_line.h`](./get_next_line.h) in your `main.c`:
27 ```c
28 #include "get_next_line.h"
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 int main(void)
35 {
36 int fd;
37 char *line;
38
39 fd = open("get_next_line.c", O_RDONLY);
40 while ((line = get_next_line(fd)) != NULL)
41 {
42 printf("%s", line);
43 free(line);
44 }
45 close(fd);
46 return (0);
47 }
48 ```
49
503. Compile it:
51 ```bash
52 cc -Wall -Wextra -Werror get_next_line.c get_next_line_utils.c main.c
53 ```
54
55<h1 id="resources">Resources</h1>
56
57<h3 id="references">References</h3>
58
59- `man 2 read`
60- Peer discussions
61- [Using ssize_t vs int](https://stackoverflow.com/questions/19224655/using-ssize-t-vs-int)
62- [How do I find the maximum stack size?](https://stackoverflow.com/questions/7535994/how-do-i-find-the-maximum-stack-size)
63
64<h3 id="llm_usage">LLM Usage</h3>
65
66LLMs were used for:
67
68- General guidance during project development, primarily to make code more idiomatic
69- Parts of this README
70
71<h3 id="tests">Tests</h3>
72
73This project was tested against [gnlTester](https://github.com/Tripouille/gnlTester). Kudos to [@Tripouille](https://github.com/Tripouille) for providing this test suite.