Skip to content

πŸ“– C function that reads one line at a time β€” because read() alone was too peaceful πŸ˜… 🧠 Mastering static variables, memory management, and the art of not leaking 🧼

Notifications You must be signed in to change notification settings

Frontendab/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“œ get_next_line

Reading a line from a file descriptor is far too tedious… so let’s fix that 😎


🧠 Introduction

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 ⚑


🎯 Goals

  • 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)

βš™οΈ Mandatory Part

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

🧩 Behavior

  • 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 \n must 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 β†’ NULL is returned.
  • Must work for both file input and standard input (stdin).

πŸ’‘ Compilation

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.c
  • BUFFER_SIZE defines 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_SIZE flag.

Example:

cc -Wall -Wextra -Werror -D BUFFER_SIZE=100 main.c get_next_line.c get_next_line_utils.c

🧱 Project Structure

get_next_line/
β”‚
β”œβ”€β”€ get_next_line.c
β”œβ”€β”€ get_next_line_utils.c
β”œβ”€β”€ get_next_line.h
β”œβ”€β”€ Makefile
└── main.c  (for your tests)

πŸ§ͺ Example Test

#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);
}

πŸ“¦ Makefile Rules

Rule Description
all Compiles the project
clean Removes object files
fclean Removes object files and binary
re Cleans and rebuilds everything

🧹 Norm & Memory

  • Code must comply with the 42 Norm 🧾
  • No memory leaks or double frees allowed ❌
  • No unexpected crashes (segfaults, bus errors, etc.) 🚫

🧾 Evaluation Checklist

βœ… 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

gif

πŸ§‘β€πŸ’» Author

Ayoub Sadouri

42 Network – get_next_line project

β€œReading files one line at a time... because why make life easy?” πŸ€“

πŸ“¬ Contact

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!

About

πŸ“– C function that reads one line at a time β€” because read() alone was too peaceful πŸ˜… 🧠 Mastering static variables, memory management, and the art of not leaking 🧼

Topics

Resources

Stars

Watchers

Forks

Languages