May it be a file, stdin, or even later a network connection, you will always need a way to read content line by line. It is time to start working on this function, which will be essential for your future projects.
This project will not only allow you to add a very convenient function to your collection, but it will also make you learn a highly interesting new concept in C programming: static variables.
[line:01]
[line:02]
[line:03]
[line:04] @@@@@@@* ,@@@@@@ @@@@@@@
[line:05] @@@@@@@ ,@@@ @@@@@@@
[line:06] &@@@@@@# ,@ @@@@@@@
[line:07] @@@@@@@ ,@@@@@@@
[line:08] %@@@@@@% @@@@@@@.
[line:09] @@@@@@@ @@@@@@@
[line:10] @@@@@@@@@@@@@@@@@@@@@, ,@@@@@@@ @
[line:11] @@@@@@@@@@@@@@@@@@@@@, ,@@@@@@@ @@@@
[line:12] @@@@@@@, ,@@@@@@@ @@@@@@
[line:13] @@@@@@@,
[line:14] @@@@@@@,
[line:15]
[line:16]
[line:17]
- Clone the project.
git clone [email protected]:ricardoreves/42-get-next-line.git
- Navigate to the project directory.
cd 42-get-next-line
- implementation of
get_next_line()
function
#include "get_next_line.h"
int main(int argc, char **argv)
{
int i;
int fd;
char *line;
if (argc != 2)
printf("Error: a file path is required\n");
else
{
i = 0;
fd = open(argv[1], O_RDONLY);
while (++i)
{
line = get_next_line(fd);
if (!line)
break ;
printf("[line:%.2d]%s", i, line);
free(line);
}
close(fd);
}
return (0);
}
- Compile program and create executable
gcc -Wall -Werror -Wextra main.c get_next_line.c get_next_line_utils.c -D BUFFER_SIZE=42 -o prog
- Run program with a file path
./prog file-to-read.txt
- linux.die.net - open(3) Linux manual page
- linux.die.net - read(3) Linux manual page
- geeksforgeeks.org - Static Variables in C
- geeksforgeeks.org - Input-output system calls in C | Create, Open, Close, Read, Write
- github.com - gnlTester (2019+). Tester for the get next line project of 42 school (with personalized leaks checking on mac, using valgrind on linux)
Distributed under the MIT License. See LICENSE for more information.