My implementation of the get_next_line function, similar to the getline from CPP and fgets from C.
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 or here for the full image.
- Unix logic
- Rigor
- Unix
- Algorithms & AI
Follow the steps below
# Clone the project and access the folder
git clone https://github.com/wwwwelton/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:
clang 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 | |
---|---|---|
free_ptr | Free the memory and sets to NULL a pointer of type **char. | |
extract_line | Extracts a string from a string delimited by \n. | |
read_file | Read a file and save the result in a buffer. | |
get_line | Returns a string from a buffer. | |
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_strdup | Returns a pointer to a null-terminated byte string, which is a duplicate of the string. | |
ft_substr | Converts the string to an integer (type int). | |
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.
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Made by Welton Leite 👋 See my linkedin