"Why stick to standard file I/O when you can create your own efficient line reader?" - Every 42 Student Ever π
Welcome to my implementation of get_next_line
, a function that reads a file descriptor line by line. This project dives deep into file handling, static variables, memory management, and system calls.
- Reads a file one line at a time.
- Handles multiple file descriptors simultaneously (Bonus).
- Uses dynamic memory allocation for efficient processing.
- Includes robust error handling for edge cases like invalid FDs, EOF, and memory failures.
- Norm-compliant and well-structured code.
- Reads one line at a time: Includes the newline (
\n
) if present. - Returns NULL: When EOF is reached or on an error.
- Supports dynamic buffer sizes: Controlled via the
BUFFER_SIZE
macro.
read()
: Reads data from a file descriptor into a buffer.malloc()
: Allocates memory dynamically for buffers and lines.free()
: Frees allocated memory to avoid leaks.
- Reading files line by line.
- Handling standard input (
stdin
). - Dealing with valid and invalid file descriptors.
- Edge cases like empty files, very large lines, and dynamic buffer sizes.
#include "get_next_line.h"
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd = open("example.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)) != NULL)
{
printf("%s", line);
free(line);
}
close(fd);
return 0;
}
- Simultaneous file descriptors: Handles multiple open files without losing the reading state of each.
- Single static variable per file descriptor: Uses a static array indexed by the FD.
#include "get_next_line_bonus.h"
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd1 = open("file1.txt", O_RDONLY);
int fd2 = open("file2.txt", O_RDONLY);
char *line1;
char *line2;
line1 = get_next_line(fd1);
line2 = get_next_line(fd2);
printf("File1: %s", line1);
printf("File2: %s", line2);
free(line1);
free(line2);
close(fd1);
close(fd2);
return 0;
}
git clone https://github.com/yomazini/42Cursus-get_next_line.git
cd 42Cursus-get_next_line
- Main Function:
get_next_line()
- Helper Functions:
read_line()
: Reads the file into a buffer until a line is complete.extract_line()
: Extracts the next line and updates the static buffer.- Utility functions like
ft_strlen
,ft_strjoin
,ft_strdup
, andft_substr
for string operations.
- A static variable is used to store leftover data from previous reads.
- In the bonus part, an array of static variables is used to handle multiple FDs simultaneously.
- All dynamically allocated memory is freed to prevent leaks.
- The buffer and any unused data are freed before returning NULL.
-
Basic File with Newlines:
- File content:
Line 1 Line 2 Line 3
- Expected output:
Line 1\n Line 2\n Line 3
- File content:
-
Empty File:
- Expected output:
NULL
.
- Expected output:
-
Very Large Lines:
- Test with a file containing a single line larger than
BUFFER_SIZE
.
- Test with a file containing a single line larger than
-
Multiple FDs (Bonus):
- Read from two files alternately.
- Small
BUFFER_SIZE
:- Set
BUFFER_SIZE=1
and test splitting lines character by character.
- Set
- Binary Files:
- Use
get_next_line
on a binary file and observe behavior.
- Use
- Interrupted Reads:
- Simulate read interruptions using signals or non-blocking I/O.
- Advanced file I/O handling.
- Static variables and their behavior across function calls.
- Efficient memory management in C.
- Handling edge cases like EOF, invalid FDs, and large files.
- Writing modular, reusable code for real-world applications.
- 42 Tester for get_next_line
- Custom test cases with different
BUFFER_SIZE
values (Go crazy).
- Reads the file in chunks using
read()
. - Stores leftover data in a static variable.
- Extracts and returns one line per call.
- Cleans up memory when EOF is reached.
read()
: Fetches file data into the buffer.malloc()
: Dynamically allocates memory for buffers and lines.free()
: Releases allocated memory.close()
: Closes file descriptors.
Feel free to:
- Open issues
- Submit pull requests
- Provide feedback
Made with βοΈ and perseverance by Youssef Mazini (ymazini)
"In files we trust, one line at a time!" π