- Introduction
- Notion
- The Project
- Unix
- Rigor
- Algorithms & AI
This project will not only allow the student to add a very convenient function to your collection, but it will also make they learn a highly interesting new concept in C programming: static variable.
As the project's name suggests this is a function that will read a file descriptor and get the next line from it. Each function's call will return one line at a time but if there is nothing else to read or if an error ocurred, it should return NULL.
- Memory Allocation
- File Descriptors
- Read and Open functions
- Static and Global Variables
In my opinion, this is one of the projects that truly gave me the foundation of programming in C. The reason is that, after finishing get next line function is possible to see what is inside a file. Since you know what is inside a file it is also possible to delete it, update it or perform any action you want based on what is inside that file. In addition to this, we have to learn about, read and open functions. These are the functions that opens a program to external resources. Through them and also file descriptors is possible to manipulate many things
# Clone the repository
https://github.com/adrianofaus/Projects_42_School.git
# Access the folder
cd Projects_42_School/2.Get_next_line
# Compile your main with get_next_line files
clang get_next_line.c get_next_line_utils.c get_next_line.h main.c
# Run the binary
./a.out
It will open the sample.txt file and read each line from this file until EOF.
#include <stdio.h>
#include <fcntl.h>
#include "get_next_line.h"
int main(void)
{
char *tmp;
int fd;
fd = open("sample.txt", O_RDONLY);
tmp = get_next_line(fd);
while(tmp)
{
printf("%s", tmp);
free(tmp);
tmp = get_next_line(fd);
}
close(fd);
return (0);
}