Skip to content

Function to read lines from a file descriptor one-by-one using static variables.

Notifications You must be signed in to change notification settings

Abdalrhman-Olimat/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

Get Next Line - Efficient Line Reader

get_next_line_badge.png

Introduction

Welcome to my Get Next Line project! 🚀 Get Next Line (GNL) is a C function that reads a line from a file descriptor, making it easier to handle input from files or standard input one line at a time. This project demonstrates proficiency in file handling, dynamic memory management, and string manipulation, all essential skills for C programming.

Table of Contents

Features

  • Efficiently reads lines from files or standard input.
  • Manages dynamic memory allocation and cleanup.
  • Adheres to the 42 School standards for project submissions.

Core Functionality

  • *get_next_line:Reads the next line from the file descriptor and returns it as a string.
  • ft_read: Reads data from the file descriptor into a buffer and appends it to the existing saved string until a newline is found or the end of the file is reached.
  • cut_a_line: Extracts a line from the saved string up to and including the newline character.
  • cut_the_line: Adjusts the saved string to remove the line that was just read, preparing it for the next call to get_next_line.

Helper Functions

To support the core functionality, several helper functions are implemented:

  • ft_strlen: Calculates the length of a string.
  • check_for_newline: Check if the string contains a \n.
  • ft_strjoin: Concatenates two strings into a new string.

Getting Started

Installation

To install Get Next Line, follow these steps:

Clone the repository:

git clone https://github.com/Abdalrhman-Olimat/get_next_line.git

Compile the code:

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

Usage

Once you've linked the library to your project, you can use get_next_line to read and parse text files line by line. Here's an example of how to use get_next_line:

#include "get_next_line.h"

int	main(void)
{
        int             fd;
        char    *line;
        fd = open("example.txt", O_RDWR);
        if (fd < 0)
        {
                perror("Error opening file");
                return (1);
        }
		
        while ((line = get_next_line(fd)) != NULL)
        {
                printf("%s", line);
                free(line);
        }
        close(fd);
        return (0);
 }
}

This will read the file example.txt line by line and print each line to the console.

Contributing

Contributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request.

Contact

If you have any questions or suggestions, please reach out to me via gmail or Linkedin

About

Function to read lines from a file descriptor one-by-one using static variables.

Topics

Resources

Stars

Watchers

Forks

Languages