FGETS(3P)

🌸 FGETS(3P) β€” POSIX Programmer's Manual 🌸

okay bestie, we're doing fgets today and honestly? she's iconic. she reads a string from a stream, she's the safe girlie of the input functions, and she deserves her flowers. this page lives in the POSIX Programmer's Manual, iykyk. πŸ’…


🫢 PROLOG

so like, real talk before we start: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. yes that's a whole mood but it's important, don't @ me.


✨ NAME

fgets β€” get a string from a stream


πŸŽ€ SYNOPSIS

#include <stdio.h>

char *fgets(char *restrict s, int n, FILE *restrict stream);

πŸ’– DESCRIPTION

okay so first thing: the functionality described on this reference page is aligned with the ISO C standard. and if there's any conflict between the requirements described here and the ISO C standard, that conflict is unintentional β€” no drama intended, fr fr. this volume of POSIX.1‐2017 defers to the ISO C standard. periodt.

now the actual tea β˜• β€” 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. 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. she leaves your buffer alone, she's polite like that. πŸŽ€

also, the fgets() function may mark the last data access timestamp of the file associated with stream for update. and that last data access 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 it's a whole group project and everyone's on the list. πŸ‘‘


πŸ”₯ RETURN VALUE

upon successful completion, fgets() shall return s. slay.

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. 😭

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. that's the vibe check failing, ngl.


πŸ’€ ERRORS

refer to fgetc().

the following sections are informative. (so like, they're the bonus content, not the binding rules ✨)

🌈 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. (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.)

#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);
...

like, look at her. allocating line_max + 1 because she leaves room for the null byte, checking the return value, freeing at the end. that's main character energy right there. πŸ‘‘


🧚 APPLICATION USAGE

none.

(yes bestie, literally none. we're blessed. πŸ™ˆ)


⭐ RATIONALE

none.

(also none. the committee was chill. ✨)


🌸 FUTURE DIRECTIONS

none.

(nothing's changing, she's staying exactly as she is. periodt. πŸ’œ)


πŸ’œ 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>. go read the whole friend group, they're all connected. 🫢


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. 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. 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 .


okay that's the whole page bestie, we ate and left no crumbs πŸ’…βœ¨ β€” this was IEEE/The Open Group, 2017, and the page is FGETS(3P). bye bestie, go read some lines safely, ily πŸ«ΆπŸŽ€

πŸ“¦ where this page comes from