Scroll to navigation

getline(3) Library Functions Manual getline(3)

NOM

getline, getdelim - Saisie de chaîne délimitée

BIBLIOTHÈQUE

Bibliothèque C standard (libc, -lc)

SYNOPSIS

#include <stdio.h>
ssize_t getline(char **restrict lineptr, size_t *restrict n,
                FILE *restrict stream);
ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
                int delim, FILE *restrict stream);

Exigences de macros de test de fonctionnalités pour la glibc (consulter feature_test_macros(7)) :

getline(), getdelim() :


Depuis la glibc 2.10 :
_POSIX_C_SOURCE >= 200809L
Avant la glibc 2.10 :
_GNU_SOURCE

DESCRIPTION

getline() lit une ligne entière dans stream et stocke l'adresse du tampon contenant le texte dans *lineptr. Le tampon se termine par un octet NULL et inclut le caractère saut de ligne, si un tel séparateur a été trouvé.

If *lineptr is set to NULL before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed.

Alternativement, avant d'appeler getline(), *lineptr peut contenir un pointeur vers un tampon de *n octets alloué par malloc(3). Si le tampon n'est pas suffisant pour stocker la ligne saisie, getline() le redimensionnera avec realloc(3), mettant à jour *lineptr et *n comme il se doit.

Quoi qu'il en soit, en cas de succès, *lineptr et *n seront mis à jour afin de rendre compte respectivement de l'adresse et de la taille du tampon.

getdelim() fonctionne comme getline(), si ce n'est qu'un séparateur différent du saut de ligne peut être spécifié en tant qu'argument delimiter. Tout comme avec getline(), aucun séparateur n'est ajouté s'il n'y en avait pas dans l'entrée avant que la fin du fichier ne soit atteinte.

VALEUR RENVOYÉE

On success, getline() and getdelim() return the number of characters read, including the delimiter character, but not including the terminating null byte ('\0'). This value can be used to handle embedded null bytes in the line read.

Both functions return -1 on failure to read a line (including end-of-file condition). In the event of a failure, errno is set to indicate the error.

If *lineptr was set to NULL before the call, then the buffer should be freed by the user program even on failure.

ERREURS

Paramètres erronés (n ou lineptr valent NULL, ou bien stream n'est pas valable).
Échec de l'allocation ou de la réallocation du tampon de ligne.

ATTRIBUTS

Pour une explication des termes utilisés dans cette section, consulter attributes(7).

Interface Attribut Valeur
getline(), getdelim() Sécurité des threads MT-Safe

STANDARDS

getline() tout comme getdelim() sont des extensions GNU. Elles ont été standardisées dans POSIX.1-2008.

EXEMPLES

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{

FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
if (argc != 2) {
fprintf(stderr, "Utilisation : %s <fichier>\n", argv[0]);
exit(EXIT_FAILURE);
}
stream = fopen(argv[1], "r");
if (stream == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
while ((nread = getline(&line, &len, stream)) != -1) {
printf("Retrieved line of length %zd:\n", nread);
fwrite(line, nread, 1, stdout);
}
free(line);
fclose(stream);
exit(EXIT_SUCCESS); }

VOIR AUSSI

read(2), fgets(3), fopen(3), fread(3), scanf(3)

TRADUCTION

La traduction française de cette page de manuel a été créée par Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org> et Frédéric Hantrais <fhantrais@gmail.com>

Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.

Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french@lists.debian.org.

5 février 2023 Pages du manuel de Linux 6.03