Skip to content

get next line reads a file or input stream line by line using read(), handling buffers and multiple file descriptors in pure C.

Notifications You must be signed in to change notification settings

ayeshamk23/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

📄 get_next_line

A line-by-line file reading function for C using static variables and buffer management.

📌 Description

get_next_line is a 42 School project that implements a function to read a file or input stream one line at a time. It must handle multiple file descriptors, manage buffers efficiently, and return each line including newline characters where appropriate.

The challenge is to build this functionality without using standard library functions like fgets() or getline(), focusing instead on file descriptors, memory handling, and string operations.

🧠 Function Prototype

char *get_next_line(int fd);

⚙️ Features

  • Reads a file or standard input line by line
  • Supports multiple file descriptors simultaneously
  • Efficient buffer management using read()
  • Custom string manipulation functions allowed only from libft

🚀 Compilation

To compile your project with get_next_line.c, include:

gcc -Wall -Wextra -Werror get_next_line.c get_next_line_utils.c -D BUFFER_SIZE=42

You can change BUFFER_SIZE to test how the function behaves with different buffer lengths.

🧪 Example

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

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

📘 Concepts Learned

  • 🔁 Loop-based reading using read()
  • 🧵 Static variables for persistent state between function calls
  • 🧠 Manual memory management (malloc, free)
  • 📂 File I/O using file descriptors
  • ✂️ Custom string manipulation (ft_strjoin, ft_strdup, etc.)
  • ⚙️ Edge case handling (EOF, empty lines, NULL return)
  • 🔀 Managing multiple open files at once

About

get next line reads a file or input stream line by line using read(), handling buffers and multiple file descriptors in pure C.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages