A C function that reads any valid file line by line until the end from a file descriptor.
get_next_line is an individual project at 42 that teaches you how to open/write data in a file, and how to manage memory (does not contain memory leaks).
The goal is to get better at C, and understand memory management. You can only use those three standard library functions:
- read
- malloc
- free
, but you can also use functions from your personal library.
To make code even better, I decided to implement the function in less than 25 lines (we have a norm of coding at 42 School, such as 1 function should have only 25 lines of code).
I added a file called test.c, it takes a file name as an argument, opens it and passes the file descriptor (fd) to get_next_line until get_next_line returns -1 or 0.
Note: get_next_line returns -1, 0, 1 depending on wether an error has occurred, the reading has been completed or a line has been read respectively.
Download/clone this repo.
Get into it and build the library:
cd get_next_line
git clone https://github.com/asansyzb/get_next_line libft
make -C libft/
Build the executable:
gcc -Wall -Wextra -Werror -I./libft/ -L./libft -lft -o gnl get_next_line.c test.c
-I tells the compiler where your library header files are. ./libft/
in this case
-L tells it where your library resides. ./libft
here
-l takes the name of your library. This is the set of characters that come after lib
in your library name.
NOTE: -L and -l might look a little bit too much, you could replace those flags with libft/libft.a
if you want.
Alright, the last command created a gnl
executable in your directory. Now test it with:
./gnl sample.txt
It should read the whole file to you. Kinda like a basic cat
implementation.
NOTE: The poem in the text file is Abai Qunanbaiv's famous Black Of My Eye ;)
You can do as above and use the test.c
file I added. This is a pretty rudimentary test, if you want to try some deep tests, let me introduce you to the amazing 42FileChecker by [@jgigault][18].
Assuming you read the project instructions and coded your own get_next_line
-
Go back to the root directory and download @jgigault's 42FileChecker:
cd .. git clone https://github.com/jgigault/42FileChecker
-
Go into the test folder and run the test:
cd 42FileChecker sh 42FileChecker.sh
Press 5
to select tests for get_next_line, press 1
to install moulitest as an external repo, then 1
to configure the tests, now you handle the path to your get_next_line and then choose which test to run on your project.
All of the instructions I used was copied/paraphrased from other 42 student.
Have fun :)