FUNCTION TO GET LINE FROM FILE DESCRIPTOR
_The goal of this project is to create the function get_next_line.c which, when called in a loop, will then allow the available text in the file descriptor to be read one line at a time until the end of the file. The program must compile with the flag-D BUFFER_SIZE=xx which will be used as the buffer size for the read calls in get_next_line.
Get Next Line is an individual project at 42 that requires us to create a function similar to the getline from CPP and fgets from C. This function allows a file to be read line after line if called in a loop.
Click here for the interactive link.
- Unix logic
- Rigor
- Unix
- Algorithms & AI
Follow the steps below
# Clone the project and access the folder
git clone https://github.com/abdulazizabduvakhobov/GNL && cd GNL/
# Create a main file
touch main.c
/*
** Example of a main, change "myfile.txt"
** to a file you want to read
*/
#include <stdio.h>
#include <fcntl.h>
#include "get_next_line.h"
int main(void)
{
char *temp;
int fd;
fd = open("myfile.txt", O_RDONLY);
while(1)
{
temp = get_next_line(fd);
if (!temp)
break ;
printf("%s", temp);
free(temp);
}
return (0);
}
# Compile the files, example:
gcc get_next_line.c get_next_line.h get_next_line_utils.c main.c
# Execute your program
./a.out
# Well done!
The functions present in the utils are from the Libft with some code optimizations.
Name | Description | |
---|---|---|
ft_clear_backup | Free the memory and sets to NULL a pointer of type **char. | |
ft_update | Joins all slices of backup and returns it | |
ft_init_string | Creates new string and return the result string | |
get_next_line | Reads a line from a file descriptor. | |
Name | Description | |
ft_strlen | Computes the length of the string but not including the terminating null character. | |
ft_substr | Creates new string from start parameter to length | |
ft_strchr | Returns a substring from the string 's'. The substring begins at index 'start' and is of maximum size 'len'. | |
ft_strjoin | Returns a new string, which is the result of the concatenation of 's1' and 's2'. |
The project is regularly updated with bug fixes and code optimization.
You can use any of this third party testers to test the project