Get Next Line is one of the three project in the 42 program second circle. It aims to introduce the students to 2 new concept of C language, the File Descriptor and the static variable by creating a function to read line by line a file.
According to the subject there is 3 contraints for this project:
- The global variables are prohibited.
- The lseek function is forbidden.
- The whole files of the projects need to follow 42 norm (Link)
- read
- malloc
- free
- A int containing the fd of the file to read.
- The next readable line ended by a "\n" if that's not the last line of the file.
- if the next readable line is empty the function return NULL
Description | Mark |
---|---|
Use one or less static variable. | ✅ |
Be able to read multiple File Descriptor at the same time. | ✅ |
- Include Get Next Line Header " get_next_line.h " in your main file
- Print the return of the function.
- Compile your files with:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 <files>.c
- Launch the compiled program.
Here a simple main exemple
#include "get_next_line.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("testfile.txt", O_RDONLY);
if (fd <= 2)
{
write(2, "No such file\n", 14);
return 1;
}
char *buffer = NULL;
buffer = get_next_line(fd);
while (buffer != NULL)
{
printf("%s", buffer);
free(buffer);
buffer = get_next_line(fd);
}
close(fd);
}