This project was created as part of the 42 curriculum by svalero.
- Author:
Sergio Valero. - Languages:
You are reading the 🇬🇧 English version // También disponible en 🇪🇸 Español (leer aquí) - Source code and test files:
This project, along with others, is publicly available on GitHub under the username @samonios.
🚀 Objective.
Get Next Line is a project that involves implementing a function capable of reading from a file descriptor (a file or stdin) and returning its contents one line at a time, while preserving any data that has been read but not yet consumed until it is returned to the program.
A line is defined as a sequence of bytes ending with the
\ncharacter. If this character is not present, the sequence ends when the end of the file is reached.
📝 Details.
The mandatory part must handle a single file descriptor. The bonus part (files with the _bonus suffix) extends this functionality to support multiple file descriptors simultaneously, while maintaining independent state for each one.
Both implementations must return NULL when an error occurs or when there is no more content to read.
- Allowed functions:
read(),malloc(), andfree(). lseek(), global variables, and the libft project are not allowed.- The buffer size is managed through the
BUFFER_SIZEmacro, which is set to 42 by default. This value can be changed at compilation time.
The reading algorithm was developed using persistent storage to preserve data that has been read but not yet returned to the program. The reading process follows these steps:
- Check the stored content. Before performing a new read operation, check whether a complete line is already available in storage.
- Read new data. If no
\ncharacter is present yet, useread()to retrieve a block of up toBUFFER_SIZEbytes. - Expand the storage. When the available space is insufficient, allocate a larger memory block and copy the content that still needs to be preserved.
- Locate the end of the line. The process continues until a
\ncharacter is found, the end of the file is reached, or an error occurs. - Return the line. Allocate memory to return the first complete line, including
\nwhen present. If the end of the file is reached and some data remains, return the final fragment even if it does not end with\n. - Preserve the remaining data. Move the bytes that have not yet been returned to the beginning of the storage so they can be used in the next call.
In the mandatory part, this storage is managed through a static structure that maintains the reading state between calls to get_next_line().
The bonus part follows the same procedure, but the storage is organized as a linked list. Each node maintains the independent state of a file descriptor, allowing calls to alternate between different descriptors without mixing their contents.
The solution is based on separating data reading from line delivery. The content obtained through read() is kept in memory until a complete line can be returned. This prevents data following a newline character within the same read operation from being lost.
Using dynamic storage makes it possible to handle lines whose length exceeds BUFFER_SIZE, without imposing a fixed limit on the length of a line.
Furthermore, preserving pending data between calls avoids reading again information that is already available in memory. This way, each call returns a single line while keeping the remaining content ready for the next call.
In the mandatory part, a static variable preserves the state between calls without relying on global variables. In the bonus part, the linked list allows the same state to be maintained independently for each file descriptor.
⚙️ Compilation
The project is compiled using cc with the following compiler options (flags) to detect potential errors and warnings:
| Flag | Meaning | Purpose |
|---|---|---|
-Wall |
All warnings | Enables a broad set of compiler warnings. |
-Wextra |
Extra warnings | Enables additional warnings not included in -Wall. |
-Werror |
Warnings as errors | Treats warnings as errors. If any warning occurs, compilation is prevented. |
Optionally, the default value of BUFFER_SIZE can be changed:
| Flag | Meaning | Purpose |
|---|---|---|
-D |
Define macro | Assigns a value to a preprocessor macro before compilation. |
Before running the following commands, clone the entire repository from GitHub. If you do not want to assign a specific value to the macro, omit -D BUFFER_SIZE=100 and the default value, 42, will be used.
Run the commands from the repository root directory.
✅ Compiling the mandatory part
The main.c file contains a test program that opens a file, repeatedly calls get_next_line(), prints each line, and frees the allocated memory.
cc -Wall -Wextra -Werror -D BUFFER_SIZE=100 Vogsphere/get_next_line.c Vogsphere/get_next_line_utils.c \
Test_Files/main.c -o get_next_line🔒⭐ Compiling the bonus part
The main_bonus.c file works in a similar way, but allows multiple files to be processed simultaneously. To compile this version, use the files with the _bonus suffix:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=100 Vogsphere/get_next_line_bonus.c Vogsphere/get_next_line_utils_bonus.c \
Test_Files/main_bonus.c -o get_next_line⛔ Do not compile both implementations simultaneously, as they define the same function and this would cause conflicts.
Provide the path of the file or files you want to use as arguments. If no arguments are provided, you can enter data through standard input (stdin).
./get_next_line /path/to/test_file.txt- Official 42 Madrid subject: Get Next Line, version 14.0 (September 2026).
- C and POSIX manuals for reference on the
read(),open(), andclose()functions.
Use of AI
In accordance with the 42 curriculum guidelines, AI tools (ChatGPT) have been used as a reference to clarify technical concepts, particularly those related to file descriptors and the use of static variables. They have also been used to assist with writing and structuring this README.
The implementation and development of the project are entirely the author's own work.