Reading a line on a fd is way too tedious
A C function that returns a line ending with a newline, read from a file descriptor
Report Bug
·
Request Feature
Table of Contents
This project is focused on writing our own function to get a line from a file descriptor, without knowing its size beforehand
- We must implement a buffer. We can't neither come back nor move in the file descriptor.
- We can't use lseek, only read, malloc, free, nor the libft
- We can't use globals variables nor FILE structure, but static variables are allowed
- The buffer size used by read can be changed
- Call the function in a loop will then allow to read the text available on a file descriptor one line at a time until the end of the text, no matter the size of either the text or one of its lines
There were also two additional bonus features
- Be able to read multiple file descriptors at the same time
- Only use one static variable
Because it's a simple C function, there isn't much to say here
Having a C compiler like cc, gcc or clang
- Clone the repo
git clone https://github.com/Link-Wolf/get_next_line.git
- Include get_next_line in your C project
#include "get_next_line/get_next_line_bonus.h"
- Compile your project with the get_next_line sources
gcc [your_project.c] get_next_line_bonus.c get_next_line_utils_bonus.c -D BUFFER_SIZE=<size>
Use the get_next_line function to read any file line by line !
int fd = open("my_awesome_file", 0);
char *line = get_next_line(fd);
while (line)
{
printf("Next line : %s\n", line);
line = get_next_line(fd);
}
printf("End of file\n");Output (cat -e)
Next line : my_first_line$
Next line : my_second_line$
Next line : my_third_line$
Next line : my_fourth_line$
. . .
End of file$
- Add bonus features
See the open issues for a full list of proposed features (and known issues).
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
