Skip to content

Efficient line-by-line file reader using dynamic memory and static buffer management. Developed during the 42 school curriculum to explore low-level file I/O in C.

License

Notifications You must be signed in to change notification settings

beratbosnak/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📄 get_next_line – Read a Line From a File Descriptor in C

get_next_line License Made with C

📑 Table of Contents


📌 Project Description

get_next_line is a function that reads a line from a file descriptor, returning it one line at a time — each call continues from where the last one left off.

This project focuses on understanding file I/O in C, managing memory safely, and handling static variables across function calls.

🧠 This project was built to strengthen my low-level programming skills in C by implementing a custom line-reading function using file descriptors and dynamic memory management.


⚙️ How It Works

  • Reads from a given file descriptor (fd) until a newline \n or EOF is found.
  • Returns a dynamically allocated string for each line.
  • Manages internal buffering and memory for multiple function calls.
  • Uses a static variable to retain state between reads.
char *get_next_line(int fd);

⚙️ Compilation

Clone the repository and compile with your desired buffer size:

git clone https://github.com/valyriasteel/get_next_line.git
cd get_next_line
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c -o gnl

🚀 Usage

Include the header and call the function:

#include "get_next_line.h"
#include <fcntl.h>
#include <stdio.h>

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

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

🧪 You can test get_next_line using your own main.c or compare its output with known test files.


📤 Expected Output

Assume file.txt contains:

Hello
World!
Test123

The output will be:

Hello
World!
Test123

🔧 Buffer Size Configuration

The BUFFER_SIZE is defined via compiler flags:

-D BUFFER_SIZE=42

You can adjust it during compilation:

cc -D BUFFER_SIZE=128 ...

🎁 Bonus Part

The bonus version supports:

  • Multiple file descriptors: reading from several files in parallel

  • Bonus files:

    • get_next_line_bonus.c
    • get_next_line_bonus.h
    • get_next_line_utils_bonus.c

Compile bonus version like this:

cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line_bonus.c get_next_line_utils_bonus.c -o gnl_bonus

Use it the same way as the main version.


📁 Project Structure

All files are located in the root directory:

get_next_line/
├── get_next_line.c              # Main implementation
├── get_next_line_utils.c        # Helper functions
├── get_next_line.h              # Header file
├── get_next_line_bonus.c        # Bonus version
├── get_next_line_utils_bonus.c  # Bonus helpers
├── get_next_line_bonus.h        # Bonus header
└── README.md                    # Documentation

📜 License

This project is licensed under the MIT License. See LICENSE for details.


📬 Contact

For questions, issues, or contributions:

📝 This project and its documentation are written in English to ensure accessibility for a global audience.


Keywords: get_next_line, 42 school project, file descriptor, C programming, memory handling, line reader

About

Efficient line-by-line file reader using dynamic memory and static buffer management. Developed during the 42 school curriculum to explore low-level file I/O in C.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages