get_next_line - @42Born2Code
A C function that reads any valid file line by line until the end.
- What is get_next_line?
- Why would I use/try it?
- How do I use it?
- How do I try it out?
What is get_next_line?
Disclaimer: There are so many easier methods of doing this by using standard C functions. But the goal here is to be able to do it by using any functions from my libft and only the standard functions
Why would I use/try it?
You probably will never have to use this function if you are not a 42 student. The goal is to get better at C. As I said above, you can only use those three standard library functions:
So it makes the project harder. But you can also use functions from your personal library.
After finishing this project, you'll definitely learn some more about static variables, pointers, arrays, linked lists (if you decide to do the bonus part), dynamic memory allocation and file manipulation.
My code is not the best, but it passed all the 42 tests successfully.
How do I use it?
I added a main file called main.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.
Alright, so first of all, download/clone this repo:
git clone https://github.com/r4meau/get_next_line
Get into it and build the library:
cd get_next_line make -C libft/
Give it some time to compile all the files.
Build the executable:
gcc -Wall -Wextra -Werror -I./libft/includes/ -L./libft -lft -o gnl get_next_line.c main.c
-I tells the compiler where your library header files are.
./libft/includes/ in this case
-L tells it where your library resides.
-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:
It should read the whole file to you. Kinda like a basic
How do I try it out?
If you are a beginner to intermediate programmer and want to hone your skills in C, I highly recommend you to give this project a try.
To do that, you just have to read the project instructions, some stuffs on there might be confusing if you are not a 42 student, but don't mind them. Just make sure you use only
free as your only available standard library functions and my/your libft functions.
I created a folder named
gnl-sample for you, it has the required files plus the testing files.
All you need to do now is cd into it and clone my Libft, if you already made your own Libft, perfect, use it:
cd gnl-sample git clone https://github.com/r4meau/libft
Build the libft library and you're done:
make -C libft/ copy make -C libft/ make -C libft/ clean
NOTE: Just a reminder, in my Libft repo, the header file
libft.h is in the
libft/ directory, not
libft/includes/, so when you build your project, use this instead:
gcc -Wall -Wextra -Werror -I./libft/ -L./libft -lft -o gnl get_next_line.c main.c
The instructions are clear enough, so I won't add anything else. If you have any questions, feel free to ask me.
How do I test my own code?
You can do as above and use the main file I added, just make sure you use your own
get_next_line.c file. This is a pretty rudimentary test, if you want to try some deep tests, let me introduce you to the amazing 42FileChecker by @jgigault.
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
At the time of writing, @jgigault is looking for a maintainer for 42FC, so he displays a message on startup, dismiss it with
1 or contact him if you think you can help.
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.