Skip to content

πŸ“œ The function get_next_line returns each line (from a fd) as a string until it encounters a newline character or reaches the end of the file.

Notifications You must be signed in to change notification settings

rmitache-ai/get_next_line

Repository files navigation

πŸ“œ get_next_line

rmitache's 42 get_next_line Score

The C function get_next_line takes lines from a file or input stream and returns each line as a null-terminated string. Up until it meets a newline character or reaches the end of the file, it retrieves the following line.

image

Usage

Requirements

Because the function is written in C, it requires the gcc compiler and a few common C libraries to run.

Simply include this header inside your file to access the function

#include "get_next_line.h"

Then include the source files and the appropriate flag when compiling your code:

get_next_line.c get_next_line_utils.c -D BUFFER_SIZE=<size>

Testing

To test your code you can use this following git repository:

Simple main function:

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

int main() {
    int fd;
    char *filename = "file.txt"; 
    fd = open(filename, O_RDONLY);
    if (fd == -1) {
        perror("Error opening file");
        return 1;
    }   
    char *line;
    while ((line = get_next_line(fd)) != NULL) {
        printf("%s\n", line);
        free(line); 
    }   
    close(fd);
    return 0;
}

Compile and run

gcc -Wall -Werror -Wextra -D BUFFER_SIZE=xx get_next_line.c get_next_line_utils.c && ./a.out

About

πŸ“œ The function get_next_line returns each line (from a fd) as a string until it encounters a newline character or reaches the end of the file.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages