This project aims code a function that returns a line, reading from a file descriptor.
- Mandatory Part - brief description on how the function should work.
- Bonus Part - brief description on bonus functionality proposed by the project.
- Files - list of files related to the project.
- Use Cases - code snippets with example on how to use it.
Through each call, the function will return a line. Using it in a loop, the function will return each line read in the file descriptor. It should return NULL if there is no line to read or an error occur.
Using only ONE static variable, the function is able to ouputs a line from differente file descriptors, without losing the reading thread on each of the file descriptors.
- get_next_line.h - header file
- get_next_line.c - the main functions
- get_next_line_utils.c - additional functions
- get_next_line_bonus.h - bonus header file
- get_next_line.bonus.c - the main bonus functions
- get_next_line_utils.bonus.c - additional functions
#include <stdio.h>
#include "get_next_line.h"
int main(void)
{
char *input;
input = get_next_line(0);
printf("%s", input);
free(input);
}#include <stdio.h>
#include <fcntl.h>
#include "get_next_line.h"
int main(void)
{
char *input;
int file_fd;
file_fd = open("file.txt", O_RDONLY);
input = get_next_line(file_fd);
printf("%s", input);
free(input);
}#include <stdio.h>
#include <fcntl.h>
#include "get_next_line.h"
int main(void)
{
char *input;
int file_fd;
file_fd = open("file.txt", O_RDONLY);
input = get_next_line(file_fd);
printf("%s", input);
free(input);
input = get_next_line(0);
printf("%s", input);
free(input);
}Copy and paste the snippets in a <main.c> file and compile with one of the lines below:
- Mandatory:
gcc main.c get_next_line.c get_next_line_utils.c
- Bonus:
gcc main.c get_next_line_bonus.c get_next_line_utils_bonus.c