get_next_line()
(GNL) is a program that is able to read from a textfile and display each line ending with a newline character. This this project done with only usingread()
,malloc()
,free()
and our own library libft we build previously.- Project done at WeThinkCode JHB campus (based on 42 france coding curriculum)
- More info on WeThinkCode: https://www.wethinkcode.co.za/
- More info on 42 school(US based school English site): https://www.42.us.org/
- More info on 42 school(France based school French site): https://www.42.fr/
- C
- Create a function that returns a line read from a file descriptor
- The following prototype must be used:
int get_next_line(const int fd, char **line);
fd = file descriptor used | line = the pointer used to store line read
- Your function
get_next_line()
must return its result without\n
- To get a full project outline, please refer to the pdf
The main goal of the project is to replicate the function fgets() to read a line from a texfile similar to the example code.
//EXAMPLE CODE
int main(int argc,char **argv)
{
FILE * x;
x=fopen("myfile.txt","r");
char hold[256]; // Step 2
while(fgets(hold,sizeof(hold),x) != NULL) // Step 1 +2
{
printf("%s",hold); // Step 3
}
fclose(x);
return (0);
}
fgets()
is function that reads a line from a stream (e.g the mytextfile.txt
in this case) at "x" number of characters at a time (i.e size of string or BUFF_SIZE
).On success, the function returns a line which is indentified by the \n
representing end of a line.The line is stored in a variable with a allocated size(i.e hold
with length of 256).When it reaches the 'End of File(EOF)' indicated by \0
it simply returns NULL
.
char *fgets(char *str, int n, FILE *stream)
To replicate fget()
GNL is broken down into the following steps:
Step1. In main.py
, the called function get_next_line()
is used read each line until EOF which returns a value of -1
while ((x = get_next_line(fd, &line2)) > 0)
Step2. In get_next_line.c
the function get_next_line()
copies characters from file descriptor into a pointer called temp
according to BUFF_SIZE
found in get_next_line.h
Here is an example for how it would work :
- If the
BUFF_SIZE = 3
line = "Hi\nMy name is Nathi"
s1=[H][i][\n](first loop)
- If no nextline character is found, the function
get_next_line()
keeps reading new characters and concatenates them withtemp
- If the nextline character is found the function
ft_cpy()
is used to create a new temp variable without the\n
and returned toft_leftover
. ft_leftover()
is used to create new pointer,allocate memory for pointer and return pointer without nextline.- Lastly, the final line 'Hi' is returned in
line2
.
Step3. In main.py
get_next_line()
returns line2
to display all lines in textfile with \n
added at the end
while ((x = get_next_line(fd, &line2)) > 0)
{
ft_putstr(line2); // function from libft to display string to standard output
ft_putchar('\n'); // function from libft to display char to standard output
free(line2);
}
git clone
repo- Enter
/get_next_line
folder git clone
libft- Enter
/libft
folder - To compile
Makefile
oflibft
type:
make re
- Go back to
/get_next_line
folder - To compile
Makefile
ofget_next_line
type:
make all
- To execute file type:
./run.a mytext.txt
You should see
aaa
bbb
ccc
displayed to standard output from myfile.txt
make re
- recompile library when changes are made to functionsmake clean
- removes all function objectsmake fclean
- removes all function objects created with library object