Statement of the project (in Spanish / English)
This project focuses on creating a function that reads a line from a file descriptor, returning the line each time it is called. This function allows you to read from files or standard input (stdin) line by line until the end of the file or an error is encountered.
-
Implement a function that reads from a file descriptor and returns the next line.
-
Manage memiry efficiently to ensure the function works correctly with different buffer sizes.
-
Handle multiple file descriptors simultaneously in the bonus part.
The prototype for the get_next_line
function is:
char *get_next_line(int fd);
-
get_next_line
: Reads from the file descriptorfd
and returns the next line. -
The function should return the line that was read. If there is nothing else to read or an error occurs, it returns 'NULL'.
-
The function should work with both files and standard input.
The function should use a static variable to managa the state between multiple calls for the same file descriptor.
Note: A static variable is a variable that retains its value between function calls. Its lifetime extends throughout the execution of the program.
The function reads BUFFER_SIZE
bytes at a time. You can define the buffer size during compilation:
cc -Wall -Werror -Wextra -D BUFFER_SIZE=42 <files>.c
For the bonus part, the function should be able to handle multiple file descriptors simultaneously without mixing up lines between them.
To achieve this, the static variable is modified to be an array:
static t_list *list[OPEN_MAX];
Note: OPEN_MAX
is a macro defined in <limits.h>
(in Mac, in Linux you must define it) representing the maximum number of files a process can have open simultaneously.
-
Reading from File Descriptor:
-
Use
read()
to readBUFFER_SIZE
bytes from the file descriptor. -
Store the read content in a buffer.
-
-
Managing Static Variable:
-
Use a static variable to retain the state between function calls.
-
Append new data to the static variable until a complete line is found.
-
-
Extracting a line:
-
Identify the end of the line (newline character '\n').
-
Extract the line from the buffer and update the static variable to retain any remaining data.
-
-
Memory Management:
-
Ensure proper allocation and deallocation of memory to prevent leaks.
-
Use dynamic memory allocation to handle variable-length lines.
-
-
Handling Edge Cases:
-
Handle end-of-file (EOF) and errors gracefully.
-
Ensure the function returns 'NULL' when there are no more lines to read or an error occurs.
-
-
Bonus part:
-
Modify the static variable to handle multiple file descriptors.
-
Ensure each file descriptor has its own buffer to manage state independently.
-