Git Product home page Git Product logo

get_next_line's Introduction

Get Next Line banner

The Get Next Line (GNL) is a function developed in C that reads text from one or more file descriptors (FDs) and returns one line at a time when calling the get_next_line function.

With this project I got the opportunity to really understand how to prevent memory leaks and how to allocate just the neccessary space in order to increase the optimization of the code. Furthermore, with the bonus files, not only you can read content from one but from as many file descriptor as you want without losing track of what line you have to return next.

Project Structure

Bonus

This project includes a bonus version that supports reading from multiple file descriptors without losing track of the lines of each file descriptor, handling them simultaneosly. For that, it uses an array of buffers. To use the bonus version, compile the *_bonus.c and get_next_line_bonus.h files.

Table of Functions

Function Description File(s)
get_next_line() Reads a line of text from a single file descriptor and returns it. get_next_line.c
๐ŸŒŸget_next_line() The bonus version improves the other one by handling multiple file descriptors without losing track of the last line returned on each file. It uses an array of pointers to manage separate buffers for different file descriptors. get_next_line_bonus.c
free_str() Frees the memory allocated for a string and returns NULL. get_next_line.c, get_next_line_bonus.c
get_result_buffer() Updates the buffer by extracting the first line and returning it. get_next_line.c, get_next_line_bonus.c
ft_add_buffer() Reads data from the file descriptor into a buffer every BUFFER_SIZE bytes and appends it to an existing buffer until it finishes reading or encounters a new line. get_next_line.c, get_next_line_bonus.c
ft_strlen() Calculates the length of a string. get_next_line_utils.c, get_next_line_utils_bonus.c
ft_strdup() Duplicates a string. get_next_line_utils.c, get_next_line_utils_bonus.c
ft_substr() Extracts a substring from a string. get_next_line_utils.c, get_next_line_utils_bonus.c
ft_gn_strchr() Searches for a character in a string and returns the position of its first occurrence. get_next_line_utils.c, get_next_line_utils_bonus.c
ft_gn_strjoin() Returns a new string formed by the concatenation of s1 and s2. get_next_line_utils.c, get_next_line_utils_bonus.c

Usage

Supported Platforms

  • Linux
  • macOS

Prerequisites

Before using Get Next Line, you need:

  • GCC: The GNU Compiler Collection.

Main Example (Bonus - multiple file descriptors)

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

int main(void)
{
    int fd1;
    int fd2;
    char *line;

    // Open files for reading
    fd1 = open("first_file.txt", O_RDONLY);
    fd2 = open("second_file.txt", O_RDONLY);

    if (fd1 == -1 || fd2 == -1)
    {
        printf("Error opening files\n");
        return (1);
    }

    // Read and print lines from fd1
    line = get_next_line(fd1);
    if (line)
    {
        printf("From fd1: %s\n", line);
        free(line);
    }

    // Read and print lines from fd2
    line = get_next_line(fd2);
    if (line)
    {
        printf("From fd2: %s\n", line);
        free(line);
    }

    // Switch back to fd1
    line = get_next_line(fd1);
    if (line)
    {
        printf("From fd1 again: %s\n", line);
        free(line);
    }

    // Close the files
    close(fd1);
    close(fd2);

    return 0;
}

Output Example:

From fd1: This is the first line from first_line.
From fd2: This is the first line from second_file.
From fd1 again: This is the second line from first_line.

get_next_line's People

Contributors

jandrana avatar

Stargazers

 avatar Lissimaka avatar  avatar Nacho Aguilar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.