FGETS(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

โœจ FGETS(3) โœจ

okay bestie so this is the fgets page from the Library Functions Manual, and lowkey it's a whole mood: it's about grabbing a line from a stream, and it also has to break the news that gets() is gone-gone. ๐Ÿ’…


๐ŸŽ€ NAME ๐ŸŽ€

fgets, gets_s โ€” get a line from a stream

like, one singular line. from a stream. that's the whole vibe check. ๐Ÿฆ‹


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C Library (libc, -lc)


โœจ SYNOPSIS โœจ

#include <stdio.h>

char *
fgets(char * restrict str, int size, FILE * restrict stream);

#define __STDC_WANT_LIB_EXT1__ 1

char *
gets_s(char *str, rsize_t size);

note the #define __STDC_WANT_LIB_EXT1__ 1 up there โ€” you literally cannot summon gets_s without it, iykyk. ๐Ÿซถ


๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so fgets() reads at most one less than the number of characters you specified with size from the stream you gave it, and stashes them in the string str. the way that "one less" detail matters โ€” that's your room for the null terminator, bestie. it stops reading when it hits a newline character, at end-of-file, or on error. and the newline? if there is one, it's retained. she stays. ๐ŸŽ€

if any characters were read and there's no error, a '\0' character gets appended to end the string. classic.

gets_s() is equivalent to fgets() with a stream of stdin, except the newline character (if any) is not stored in the string. so like, similar energy but the newline does NOT get an invite to the party. ๐Ÿ™ˆ

and then there's gets(). bestie. the gets() function was unsafe and is no longer available. she's not coming back. we don't talk about her. ๐Ÿ’€

๐Ÿ”ฅ RETURN VALUES ๐Ÿ”ฅ

upon successful completion, fgets() and gets_s() return a pointer to the string. slay. ๐Ÿ‘‘

but listen:

  • if end-of-file occurs before any characters are read, they return NULL and the buffer contents remain unchanged.
  • if an error occurs, they return NULL and the buffer contents are indeterminate (๐Ÿ˜ญ do not trust them).

and here's the main-character plot twist: fgets() and gets_s() do not distinguish between end-of-file and error, so you as the caller literally have to use feof(3) and ferror(3) to figure out which one actually happened. no cap. ๐Ÿงš


๐ŸŒธ ERRORS ๐ŸŒธ

  • [EBADF] โ€” the given stream is not a readable stream. like, she cannot be read from. that's the error. ๐Ÿ’…

The function fgets() may also fail and set errno for any of the errors specified for the routines fflush(3), fstat(2), read(2), or malloc(3).

The function gets_s() may also fail and set errno for any of the errors specified for the routine getchar(3).


๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

go read those too, they're fr fr important โ€” especially feof(3) and ferror(3), since you need them to tell eof apart from an actual error. ๐Ÿ’œ


๐ŸŒˆ STANDARDS ๐ŸŒˆ

The fgets() function conforms to ISO/IEC 9899:1999 ("ISO C99").

gets_s() conforms to ISO/IEC 9899:2011 ("ISO C11") K.3.7.4.1.

gets() has been removed from ISO/IEC 9899:2011 ("ISO C11"). yeah. removed removed. โญ


๐Ÿ‘‘ HISTORY ๐Ÿ‘‘

The functions fgets() and gets() first appeared in Version 7 AT&T UNIX. vintage icons, honestly. ๐ŸŽ€


and that's the whole page, bestie โ€” this one shipped with Debian, dated December 6, 2024, and it's FGETS(3) forever and always. mwah. ๐Ÿ’‹โœจ

๐Ÿ“ฆ where this page comes from
  • freebsd 15.1: base 15.1
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms