This project is the implementation of a function which returns a sentence separated with newline.
read
on <unistd.h>malloc
on <stdlib.h>free
on <stdlib.h>
Name | Parameters | Return |
---|---|---|
get_next_line | int fd, char **line | int |
-
The mandatory and bonus in the project can deal with the multiple fd. When the function is called on the outside of the moudule, the program can hold multiple files which are using the gnl function with file opened.
-
Every string in the gnl is freed when there is occured errors, so the line has to be freed only when there is no error.
-
The gnl function is using Buffer which perform saving values from read function. The size of Buffer depends on the defined Macro Constant. It can be set by
-D
option of gcc compiling command, or the constant will use the default value. -
The rest of the string after newline will be saved by static variable. It will not be disappeared before the terminating program or reading the End of the File.
// This is just an example of reading a line in test.txt by opening it Read-Only.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int fd;
int ret;
char *line;
fd = open("test.txt", O_RDONLY);
if (fd < 0)
return (1);
ret = get_next_line(fd, &line);
if (ret < 0)
{
close(fd);
return (1);
}
printf("Result: %d\n", ret);
printf("Sentence: %s\n", line);
free(line);
close(fd);
return (0);
}