get_next_line is a function that reads a file line by line. It is a function that is part of the 42 curriculum and is used to teach students how to use static variables and memory allocation in C.
- Clone the repository:
git clone https://github.com/abouabra/42-get_next_line.git
- Change the current working directory:
cd 42-get_next_line
To use the get_next_line function in your project, include the get_next_line.h
header file in your source code:
#include "get_next_line.h"
The get_next_line
function reads a file descriptor and returns a line ending with a newline character from a file descriptor. The function returns the line that was read with the newline character. The function returns NULL
when the reading is finished or an error occurs.
char* get_next_line(int fd);
in order to read a file line by line, you can use the following code:
#include <stdio.h>
#include <fcntl.h>
#include "get_next_line.h"
int main(void)
{
int fd;
char *line;
fd = open("example.txt", O_RDONLY);
if (fd == -1)
{
printf("Error: Could not open file\n");
return (1);
}
while (true)
{
line = get_next_line(fd);
if (line == NULL)
break;
printf("%s", line);
free(line);
}
close(fd);
return (0);
}
- Compile your source code with the
get_next_line.c
file:
gcc -Wall -Wextra -Werror -D BUFFER_SIZE=32 example.c get_next_line.c -o example
- Run the executable:
./example
- The output will be the content of the file
example.txt
printed line by line.
The BUFFER_SIZE
macro is used to define the size of the buffer used to read the file. The BUFFER_SIZE
must be greater than 0 and less than or equal to INT_MAX
(limits.h). The BUFFER_SIZE
can be defined at compile time using the -D
flag or in the source code using the #define
directive.
The get_next_line
function from the bonus version get_next_line_bonus.c
can be used to read from multiple file descriptors. The function can be used to read from standard input, a file, or a network socket.
#include <stdio.h>
#include <fcntl.h>
#include "get_next_line_bonus.h"
int main(void)
{
int fd1;
int fd2;
char *line;
fd1 = open("example1.txt", O_RDONLY);
if (fd1 == -1)
{
printf("Error: Could not open file\n");
return (1);
}
fd2 = open("example2.txt", O_RDONLY);
if (fd2 == -1)
{
printf("Error: Could not open file\n");
return (1);
}
while (true)
{
line = get_next_line(fd1);
if (line == NULL)
break;
printf("%s", line);
free(line);
line = get_next_line(fd2);
if (line == NULL)
break;
printf("%s", line);
free(line);
}
close(fd1);
close(fd2);
return (0);
}
- Compile your source code with the
get_next_line_bonus.c
file:
gcc -Wall -Wextra -Werror -D BUFFER_SIZE=32 example_bonus.c get_next_line_bonus.c -o example_bonus
- Run the executable:
./example_bonus
- The output will be the content of the files
example1.txt
andexample2.txt
printed line by line.
If you have any suggestions or improvements, feel free to open an issue or submit a pull request.