Skip to content

get_next_line is a 42 school project meant to teach us how to read from a file descriptor and use static variables.

Notifications You must be signed in to change notification settings

GarGrigoryan/get_next_line

Repository files navigation

This project has been created as part of the 42 curriculum by gargrigo.

get_next_line

Description

The get_next_line project is a fundamental challenge in the 42 common core. The objective is to write a function that returns a single line read from a file descriptor.

This project introduces the concept of static variables in C programming and requires careful management of dynamic memory allocation. The function must handle repeated calls to read a text file one line at a time until the end of the file, regardless of the buffer size or the file's length.

Features

  • Reads from files, standard input, or even network sockets.
  • Supports any BUFFER_SIZE defined at compile time.
  • Efficiently manages memory to prevent leaks (using valgrind or leaks for verification).
  • Handles multiple file descriptors simultaneously (if implemented in the bonus part).

Why Use a Static Variable?

In C, standard local variables are deallocated when a function returns. By declaring our stash as static char *stash, we ensure that any data read but not yet returned remains available for the next time get_next_line is called.


Instructions

Compilation

The project must be compiled with the -D BUFFER_SIZE=n flag to define the buffer size for the read() function.

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

Usage

To test the function, you can create a main.c that calls the function in a loop:

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

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

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

Resources

Documentation

AI Usage

AI was used in the following parts of this project:

  • README.md creation: AI assistance was used to structure and write the README.md file according to 42 School requirements, including formatting and section organization.

  • Code review and debugging: AI tools were consulted for debugging assistance when encountering compilation errors or logical issues in function implementations, particularly for edge cases and memory management.

All code implementations were written manually by the student, with AI used only for debugging assistance and understanding requirements.

About

get_next_line is a 42 school project meant to teach us how to read from a file descriptor and use static variables.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages