Skip to content

returns a line ending with a newline, which is read from a file descriptor

License

Notifications You must be signed in to change notification settings

hosuzuki/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

get_next_line


🌕 What is get_next_line ?

get_next_line is an individual project at 42 Tokyo to make a function that returns a line ending with a newline, read from a file descriptor, which will be useful to get familiar with memory allocation, static variables, and file descriptor.


get_next_line function returns one line from the specified fd every time it is called, followed by a newline ('\n') if the line has one.

🌖 Prototype

The function to implement is called get_next_line, and will have the following prototype:

char *get_next_line(int fd);

🌗 Input Parameters

The function only receives an int fd, which will be the file descriptor of an open file, or perhaps even the standard input if the file descriptor is zero.

🌗 Return Values

This function only has two possible return values:

Return Value Description
char * The string containing a line read by the function
(null) Either an error occurred or reached EOF (End Of File)

🌕 Usage

🌖 Requirements

The function is written in C language and thus needs the gcc compiler and some standard C libraries to run.

🌖 Instructions

🌗 Give it a try (use main.c in srcs)

🌘 1. Clone the repository
git clone git@github.com:hosuzuki/get_next_line.git

And them, move to the cloned directory.

cd get_next_line
🌘 2. Compile get_next_line with main.c which has test code

The makefile compiles all files from the srcs/ folders and saves the object files to the objs/ folders.
It then generates the output file gnl.

make     
🌘 3. Run gnl to see the test result
./gnl   



🌗 Using it in your code (use your main.c)

To use the function in your code, simply include its header:

#include "get_next_line.h"

and, when compiling your code, add the source files and the required flag:

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

If you're on Linux, you may as well need the following flags:

-D ARG_MAX="sysconf(_SC_ARG_MAX)" -D OPEN_MAX=1024
🌘 example usage

Calling get_next_line in a loop will allow you to read the text available on a file descriptor one line at a time until the EOF.

Call get_next_line from the main

int main(int argc, char **argv)
{
	int fd;
	char *line;

	line = NULL;
	if (argc == 2)
	{
		fd = open(argv[1], O_RDONLY);
		while ((line = get_next_line(fd)))
		{
			printf("%s", line);
			free(line);
		}
		close(fd);
	}
}



🌕 Test Samples

These files are in the "sample" folder.

Filename Description
1chara One character per line
3chara Three characters per line
empty Empty file
just_nl Just new lines
longletter Long single-line
over2k Very Long single-line file (over 2000 chars)

About

returns a line ending with a newline, which is read from a file descriptor

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published