/get_next_line

A function that reads and returns text line by line from a file, or from several files, using static variables.

Primary LanguageC


Logo

Reading a line on a fd is way too tedious.

About The Project

The aim of this project is to make you code a function that returns a line ending with a newline, read from a file descriptor.

Built With

C, with some Shell for testing purposes.

Prerequisites

  • A Shell terminal
  • gcc compiler

Installation

Clone the repo

git clone git@github.com:rkrocha/get_next_line.git

Usage

  1. Copy get_next_line.c and get_next_line.h (or their bonus counterparts) to your program's folder, and include the header in your program:
#include "get_next_line.h"
  1. A simple program would look like this:
#include <stdio.h>
#include <fcntl.h>
#include "get_next_line.h"

int	main(void)
{
	int	fd;
	int	ret;
	char	*line;

	fd = open("./path/to/text_file.txt", O_RDONLY);
	while ((ret = get_next_line(fd, &line)) >= 0)
	{
		printf("%s\n", line);
		free(line);
		if (ret == 0)
			break ;
	}
	if (ret == -1)
	{
		printf("<ERROR>\n");
		close(fd);
		return (-1);
	}
	close(fd);
	return (0);
}

OR

Test it in its own folder by running the script:

sh run_test.sh