Skip to content

AhmadNajiKam/GetNextLine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

This activity has been created as part of the 42 curriculum by akamamji.

Description

The get_next_line project is a fundamental challenge in the 42 circle that tasks students with writing a function that returns a line read from a file descriptor.

The goal is to design a function that can be called in a loop, allowing a program to read an entire text file one line at a time until the end of the file (EOF) is reached. This project introduces the concept of static variables in C and requires careful memory management to ensure that data remains persistent between function calls without causing memory leaks.

Instructions

Compilation

To use this function in your project, include the header and compile the source files with the -D BUFFER_SIZE=n flag to define the read buffer size.

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

Usage

Include the header in your C file:

#include "get_next_line.h"

Then, call the function in a loop to read a file:

int    fd;
char   *line;

fd = open("example.txt", O_RDONLY);
while ((line = get_next_line(fd)) != NULL)
{
    printf("%s", line);
    free(line);
}
close(fd);

Algorithm Explanation & Justification

The core challenge of get_next_line is handling "leftover" data. When we read a chunk of text (defined by BUFFER_SIZE), we might read past the first newline character (\n).

The Selection: Static Variable Buffer

I selected an algorithm based on a static character pointer to act as a persistent cache.

  1. Read and Append: The function reads from the file descriptor into a temporary buffer and appends it to our static "stash" until a newline character is found or EOF is reached.
  2. Extraction: Once the stash contains a newline, the function calculates the length of the line, allocates memory for it, and copies the data up to and including the \n.
  3. Cleanup (The "Remainder"): The static stash is then updated to store only the data that follows the newline, ensuring it is available for the next call to get_next_line.

Justification: This approach is the most memory-efficient way to handle arbitrary buffer sizes. By using a static variable, we avoid losing track of data between calls, which is essential because the read system call moves the file offset forward and cannot be "undone."

Resources

AI Usage Disclosure

AI (specifically Gemini/ChatGPT) was used during this project for the following:

  • Brainstorming: Clarifying the logic of pointer arithmetic when splitting the static buffer.
  • Debugging: Explaining specific "segmentation fault" scenarios related to uninitialized static pointers.
  • Documentation: Generating the initial structure and formatting of this README file.

Would you like me to refine the Technical Choices section or add a section for the Bonus requirements (handling multiple file descriptors)?

About

A clone of getline function in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages