The Get Next Line (GNL) is a function developed in C that reads text from one or more file descriptors (FDs) and returns one line at a time when calling the get_next_line function.
With this project I got the opportunity to really understand how to prevent memory leaks and how to allocate just the neccessary space in order to increase the optimization of the code. Furthermore, with the bonus files, not only you can read content from one but from as many file descriptor as you want without losing track of what line you have to return next.
get_next_line.c
: Main functions for the implementations of the get_next_line.get_next_line_utils.c
: Utility functions used for the GNL.get_next_line.h
: Header file for defining used libraries, function prototypes and constants likeBUFFER_SIZE
This project includes a bonus version that supports reading from multiple file descriptors without losing track of the lines of each file descriptor, handling them simultaneosly. For that, it uses an array of buffers. To use the bonus version, compile the *_bonus.c and get_next_line_bonus.h files.
Function | Description | File(s) |
---|---|---|
get_next_line() | Reads a line of text from a single file descriptor and returns it. | get_next_line.c |
๐get_next_line() | The bonus version improves the other one by handling multiple file descriptors without losing track of the last line returned on each file. It uses an array of pointers to manage separate buffers for different file descriptors. | get_next_line_bonus.c |
free_str() | Frees the memory allocated for a string and returns NULL. | get_next_line.c , get_next_line_bonus.c |
get_result_buffer() | Updates the buffer by extracting the first line and returning it. | get_next_line.c , get_next_line_bonus.c |
ft_add_buffer() | Reads data from the file descriptor into a buffer every BUFFER_SIZE bytes and appends it to an existing buffer until it finishes reading or encounters a new line. |
get_next_line.c , get_next_line_bonus.c |
ft_strlen() | Calculates the length of a string. | get_next_line_utils.c , get_next_line_utils_bonus.c |
ft_strdup() | Duplicates a string. | get_next_line_utils.c , get_next_line_utils_bonus.c |
ft_substr() | Extracts a substring from a string. | get_next_line_utils.c , get_next_line_utils_bonus.c |
ft_gn_strchr() | Searches for a character in a string and returns the position of its first occurrence. | get_next_line_utils.c , get_next_line_utils_bonus.c |
ft_gn_strjoin() | Returns a new string formed by the concatenation of s1 and s2 . |
get_next_line_utils.c , get_next_line_utils_bonus.c |
- Linux
- macOS
Before using Get Next Line, you need:
- GCC: The GNU Compiler Collection.
#include "get_next_line.h"
#include <stdio.h>
#include <fcntl.h>
int main(void)
{
int fd1;
int fd2;
char *line;
// Open files for reading
fd1 = open("first_file.txt", O_RDONLY);
fd2 = open("second_file.txt", O_RDONLY);
if (fd1 == -1 || fd2 == -1)
{
printf("Error opening files\n");
return (1);
}
// Read and print lines from fd1
line = get_next_line(fd1);
if (line)
{
printf("From fd1: %s\n", line);
free(line);
}
// Read and print lines from fd2
line = get_next_line(fd2);
if (line)
{
printf("From fd2: %s\n", line);
free(line);
}
// Switch back to fd1
line = get_next_line(fd1);
if (line)
{
printf("From fd1 again: %s\n", line);
free(line);
}
// Close the files
close(fd1);
close(fd2);
return 0;
}
From fd1: This is the first line from first_line.
From fd2: This is the first line from second_file.
From fd1 again: This is the second line from first_line.