Skip to content

enekomb/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

get_next_line — File Descriptor Line Reader in C

42 Urduliz — Common Core project

A C function that reads a single line from a file descriptor each time it is called — like fgets but for any file descriptor (files, stdin, sockets).


Prototype

char *get_next_line(int fd);

Returns the next line (including \n) or NULL on EOF or error. Calling it in a loop reads the entire file line by line.

Usage

#include "get_next_line.h"

int fd = open("file.txt", O_RDONLY);
char *line;

while ((line = get_next_line(fd)) != NULL)
{
    printf("%s", line);
    free(line);
}
close(fd);

How It Works

Uses a static buffer to keep leftover data between calls. Reads in chunks of BUFFER_SIZE bytes (compile-time configurable), splits on \n, and returns one line at a time.

cc -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c main.c

Skills

C Static Variables File Descriptors Memory Management Buffer Management


Academic Integrity

This repository is shared for educational and portfolio purposes only.

For 42 students:

  • Do not copy this code for your own project submissions
  • Use it as a reference to understand concepts and approaches
  • Write your own original solution — that is the only way to actually learn

42's core values are learning through practice, peer collaboration, and genuine problem-solving.


License

MIT — see LICENSE for details. Copyright (c) 2026 Eneko Muñoz Bordona

About

Line-by-line file descriptor reader in C - static buffer management | 42 Urduliz

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages