Skip to content

Vlad-PLK/GET_NEXT_LINE

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

GET NEXT LINE

In this project, I created a function that returns every next line of a text file each time we call it. The prototype looks like this :

char	*get_next_line(int fd)
{
	...
	return (line);
}

As you can see it takes a file descriptor as argument and returns a char string. The file descriptor corresponds to the open file you wanna read from. Here is an example of get_next_line function :

int	main(void)
{
	int		fd;

	fd = open("text.txt", O_RDWR);
	/// we use printf directly to display the char string returned by get_next_line ///
	printf("Here is the first line of the file text : %s", get_next_line(fd));	
}

first_example

In the picture above, you can firstly notice that we compiled with a flag "-D BUFFER_SIZE=10". BUFFER_SIZE is a variable corresponding to the number of characters read each time by read function. It means that we've made a function get_next_line that returns the first next line of a text file even if BUFFER_SIZE is a very large number or if it's equal to 1. That was a specific rule mentioned in our subject. Right after the compilation line you can see that text.txt contains 2 lines and finally right after the cat call you see that gnl returned only the first line. Here's a second example with multiple lines and multiple calls of this function (obviously, I added some lines to text.txt) :
int	main(void)
{
	int		fd;

	fd = open("text.txt", O_RDWR);
	printf("Here is the first line of the file text : %s", get_next_line(fd));
	printf("Here is the second line of the file text : %s", get_next_line(fd));
	printf("Here is the third line of the file text : %s", get_next_line(fd));
	printf("Here is the fourth line of the file text : %s", get_next_line(fd));
	printf("Here is the last line of the file text : %s", get_next_line(fd));
}

second_example

To make sure that our get_next_line function returns each time the very next line in a specific file we use a "static" variable that keeps track of the position in the text file after each function call. Here's a quick explanation from GeeksforGeeks to help you understand more static variables :

static_variables



I'll conclude this part by talking about the benefits of such a function : Indeed, now that I can consistantly return the next line of a desired file, I can use it for example in a game project where GNL will help me read my map files and parse it correctly. So did I in so_long and cub3d graphic projects. Moreover, making this function helped me understand even more how to work with strings, files, and read function.

About

Project Get Next Line 42

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages