Reading a line from a file descriptor is far too tediousβ¦ so letβs fix that π
The get_next_line project is about writing a function that returns a line read from a file descriptor β
each call gives you the next line until the file ends (or an error occurs).
Youβll also discover one of the most powerful concepts in C: static variables β‘
- Understand and use static variables π§ββοΈ
- Learn how to manage persistent data between function calls
- Practice working with file descriptors and the
read()function - Build a robust line reader for any text input (file or stdin)
| Function name | get_next_line |
|---|---|
| Prototype | char *get_next_line(int fd); |
| Files to turn in | get_next_line.c, get_next_line_utils.c, get_next_line.h |
| Parameters | fd β The file descriptor to read from |
| Return value | A line that has been read β or NULL if thereβs nothing else to read or an error occurs |
| External functions | read, malloc, free |
| Forbidden | lseek, global variables, and your libft |
- The function reads from the file descriptor one line at a time.
- Each call to
get_next_line()should return the next line. - The newline character
\nmust be included in the returned string β
except for the last line of the file if it doesnβt end with\n. - If thereβs nothing left to read or an error occurs β
NULLis returned. - Must work for both file input and standard input (stdin).
Your code will be compiled with the following command:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.cBUFFER_SIZEdefines how many bytes are read at once.- You can compile with any buffer size value (1, 42, 9999, etc.) for testing.
- You must handle compilation with or without the
-D BUFFER_SIZEflag.
Example:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=100 main.c get_next_line.c get_next_line_utils.cget_next_line/
β
βββ get_next_line.c
βββ get_next_line_utils.c
βββ get_next_line.h
βββ Makefile
βββ main.c (for your tests)#include "get_next_line.h"
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd = open("test.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)))
{
printf("%s", line);
free(line);
}
close(fd);
}| Rule | Description |
|---|---|
all |
Compiles the project |
clean |
Removes object files |
fclean |
Removes object files and binary |
re |
Cleans and rebuilds everything |
- Code must comply with the 42 Norm π§Ύ
- No memory leaks or double frees allowed β
- No unexpected crashes (segfaults, bus errors, etc.) π«
β Returns one line at a time
β Works with multiple BUFFER_SIZE values
β Correctly handles EOF and errors
β
Includes \n in output when needed
β Compiles with all required flags
β Passes Norminette
β No memory leaks or global variables
Ayoub Sadouri
42 Network β get_next_line project
βReading files one line at a time... because why make life easy?β π€
Want to collaborate or just geek out about low-level C programming? Reach out anytime!
πβ https://ayoubsadouri.com
β If this repo made your life easier, donβt forget to star it!
