c-md/srcs/utils/io/read_line.c.md
2026-01-12 16:22:13 +01:00

728 B

read_line.c

Get a line from a file. depends on POSIX getline


Includes

#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

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);
}