FGETS(3POSIX)
π FGETS π
okay bestie, welcome to fgets(3POSIX) from the POSIX Programmer's Manual π this is the group chat where we get a whole entire string out of a stream. pull up a chair. hydrate. let's go. β¨
πΈ PROLOG πΈ
okay so first thing, real talk: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β like, consult the corresponding Linux manual page for details of Linux behavior β or honestly the interface may not be implemented on Linux at all. so don't come for me if your Linux is giving something else. π
π¦ NAME π¦
fgets β get a string from a stream
β¨ SYNOPSIS β¨
#include <stdio.h>
char *fgets(char *restrict s, int n, FILE *restrict stream);
π DESCRIPTION π
so everything described on this reference page is aligned with the ISO C standard. if there's any conflict between the requirements here and the ISO C standard, that conflict is unintentional β this volume of POSIX.1-2017 defers to the ISO C standard, no drama, the ISO C standard is the main character here. π
now the actual vibe: the fgets() function shall read bytes from stream into the array pointed to by s until:
n-1bytes are read, or- a
<newline>is read and transferred tos, or - an end-of-file condition is encountered.
a null byte shall be written immediately after the last byte read into the array. periodt. π
and if the end-of-file condition is encountered before any bytes are read, the contents of the array pointed to by s shall not be changed. so don't touch it, leave it alone, it's not yours to mess with. π«Ά
also, fgets() may mark the last data access timestamp of the file associated with stream for update. and like, that timestamp shall be marked for update by the first successful execution of fgetc(), fgets(), fread(), fscanf(), getc(), getchar(), getdelim(), getline(), gets(), or scanf() using stream that returns data not supplied by a prior call to ungetc(). so many functions, all of them tapping the same timestamp. iconic behavior honestly. β
π RETURN VALUE π
upon successful completion, fgets() shall return s. slay. π
but if the stream is at end-of-file, the end-of-file indicator for the stream shall be set and fgets() shall return a null pointer. and if a read error occurs, the error indicator for the stream shall be set, fgets() shall return a null pointer, and shall set errno to indicate the error. so if you get NULL back, check your indicators bestie, something happened. π
π₯ ERRORS π₯
refer to fgetc(). that's the whole section. go read that one.
the following sections are informative. (i.e. the vibes sections, not the rules sections π§)
π EXAMPLES π
πΈ Reading Input πΈ
the following example uses fgets() to read lines of input. it assumes that the file it is reading is a text file and that lines in this text file are no longer than 16384 (or {LINE_MAX} if it is less than 16384 on the implementation where it is running) bytes long. and, like, note that the standard utilities have no line length limit if sysconf(_SC_LINE_MAX) returns -1 without setting errno. this example assumes that sysconf(_SC_LINE_MAX) will not fail. no cap. π
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#define MYLIMIT 16384
char *line;
int line_max;
if (LINE_MAX >= MYLIMIT) {
// Use maximum line size of MYLIMIT. If LINE_MAX is
// bigger than our limit, sysconf() cannot report a
// smaller limit.
line_max = MYLIMIT;
} else {
long limit = sysconf(_SC_LINE_MAX);
line_max = (limit < 0 || limit > MYLIMIT) ? MYLIMIT : (int)limit;
}
// line_max + 1 leaves room for the null byte added by fgets().
line = malloc(line_max + 1);
if (line == NULL) {
// out of space
...
return error;
}
while (fgets(line, line_max + 1, fp) != NULL) {
// Verify that a full line has been read ...
// If not, report an error or prepare to treat the
// next time through the loop as a read of a
// continuation of the current line.
...
// Process line ...
...
}
free(line);
...
π APPLICATION USAGE π
none. truly nothing. empty. the void. π
π¦ RATIONALE π¦
none. also empty. also the void. πβ¨
π FUTURE DIRECTIONS π
none. (do you see the pattern bestie. sometimes the answer is just no thoughts head empty π«Ά)
π SEE ALSO π
- Section 2.5, Standard I/O Streams
fgetc()fopen()fread()fscanf()getc()getchar()getdelim()gets()ungetc()
and from the Base Definitions volume of POSIX.1-2017: <stdio.h> π
π COPYRIGHT π
okay legal bestie moment, i need you to read this one carefully:
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
and in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. so if they disagree, the original wins. that's the rule. π«Ά
the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . (not me being the messenger of the typo reports π)
okay that's the whole page bestie, you're officially fluent in fgets(). go read a string from a stream. be careful. check for null. π
β¨
this has been fgets(3POSIX) β IEEE/The Open Group, 2017 β brought to you by the POSIX Programmer's Manual. mwah. πππ¦
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms