GithubHelp home page GithubHelp logo

42_gnl's Introduction

42_gnl

Statement of the project (in Spanish / English)

Introduction

This project focuses on creating a function that reads a line from a file descriptor, returning the line each time it is called. This function allows you to read from files or standard input (stdin) line by line until the end of the file or an error is encountered.

Project Objectives

  • Implement a function that reads from a file descriptor and returns the next line.

  • Manage memiry efficiently to ensure the function works correctly with different buffer sizes.

  • Handle multiple file descriptors simultaneously in the bonus part.

Mandatory Part

Function Prototype

The prototype for the get_next_line function is:

char *get_next_line(int fd);

Description

  • get_next_line : Reads from the file descriptor fd and returns the next line.

  • The function should return the line that was read. If there is nothing else to read or an error occurs, it returns 'NULL'.

  • The function should work with both files and standard input.

Static Variable

The function should use a static variable to managa the state between multiple calls for the same file descriptor.

Note: A static variable is a variable that retains its value between function calls. Its lifetime extends throughout the execution of the program.

Buffer Size

The function reads BUFFER_SIZE bytes at a time. You can define the buffer size during compilation:

cc -Wall -Werror -Wextra -D BUFFER_SIZE=42 <files>.c

Bonus Part

Multiple File Descriptors

For the bonus part, the function should be able to handle multiple file descriptors simultaneously without mixing up lines between them.

Static Variable Array

To achieve this, the static variable is modified to be an array:

static t_list *list[OPEN_MAX];

Note: OPEN_MAX is a macro defined in <limits.h> (in Mac, in Linux you must define it) representing the maximum number of files a process can have open simultaneously.

Implementation Steps

  1. Reading from File Descriptor:

    • Use read() to read BUFFER_SIZE bytes from the file descriptor.

    • Store the read content in a buffer.

  2. Managing Static Variable:

    • Use a static variable to retain the state between function calls.

    • Append new data to the static variable until a complete line is found.

  3. Extracting a line:

    • Identify the end of the line (newline character '\n').

    • Extract the line from the buffer and update the static variable to retain any remaining data.

  4. Memory Management:

    • Ensure proper allocation and deallocation of memory to prevent leaks.

    • Use dynamic memory allocation to handle variable-length lines.

  5. Handling Edge Cases:

    • Handle end-of-file (EOF) and errors gracefully.

    • Ensure the function returns 'NULL' when there are no more lines to read or an error occurs.

  6. Bonus part:

    • Modify the static variable to handle multiple file descriptors.

    • Ensure each file descriptor has its own buffer to manage state independently.

42_gnl's People

Contributors

eleee28 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.