Skip to content

A C function that reads a text file line by line. This project introduces the concept of static variables and file descriptor management.

Notifications You must be signed in to change notification settings

sdadak42/Get_Next_Line

Repository files navigation

Get Next Line

Description

The Get Next Line project involves writing a function that returns a line read from a file descriptor. This project introduces the concept of static variables in C and file manipulation.

Features

  • Reads a line from a file descriptor, one line at a time.
  • Handles multiple file descriptors simultaneously (Bonus).
  • Uses a single static variable (Bonus).
  • Efficient memory management.

Getting Started

Prototype

char *get_next_line(int fd);

Compilation

To compile the project, you can use the following command. You must define the BUFFER_SIZE macro.

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

For the bonus part:

gcc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line_bonus.c get_next_line_utils_bonus.c main.c -o gnl

Usage

Here is a simple example of how to use get_next_line in your code:

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

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

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

Developed by sdadak for 42 Istanbul.

About

A C function that reads a text file line by line. This project introduces the concept of static variables and file descriptor management.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages