57 lines
728 B
Markdown
57 lines
728 B
Markdown
# read_line.c
|
|
|
|
Get a line from a file.
|
|
depends on POSIX getline
|
|
|
|
---
|
|
|
|
## Includes
|
|
|
|
```c
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
|
|
#include "utils.h"
|
|
```
|
|
|
|
---
|
|
|
|
## Function Description
|
|
|
|
### `read_line`
|
|
|
|
Get a line from a file.
|
|
|
|
#### Parameter
|
|
- `f`: Pointer to a `FILE` object representing the input file.
|
|
|
|
#### Return Value
|
|
|
|
Returns a pointer to the read line (dynamically allocated) or `NULL` on failure
|
|
or end of file.
|
|
|
|
Return value must be freed by the caller.
|
|
|
|
#### Implementation
|
|
|
|
```c
|
|
char *
|
|
read_line(FILE *f)
|
|
{
|
|
char *line;
|
|
size_t len;
|
|
ssize_t read;
|
|
|
|
line = NULL;
|
|
len = 0;
|
|
read = getline(&line, &len, f);
|
|
if (-1 == read)
|
|
{
|
|
free(line);
|
|
return (NULL);
|
|
}
|
|
return (line);
|
|
}
|
|
```
|