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-1 bytes are read, or
  • a <newline> is read and transferred to s, 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> πŸ’–


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. πŸ’–πŸŽ€πŸ¦‹