fgets(3)
๐ fgets(3) โ the "get a whole line, bestie" situation ๐
okay so real talk: this is fgets from the Standard C library (libc, -lc), and it's literally the moment your girl stops nibbling characters one at a time and just gets a line. FILE get line-string. that's the whole name. iconic. ๐
โจ SYNOPSIS โจ
#include <stdio.h>
char *fgets(int size;
char s[restrict size], int size, FILE *restrict stream);
like yes that's the signature exactly as it is, i'm not touching it, my hands are OFF it ๐ซถ
๐ธ DESCRIPTION ๐ธ
okay bestie listen. fgets() reads in at most one less than size characters from stream, and it stashes them into the buffer that s points at. the <hotpink>one less than</hotpink> part is NOT a typo, it's for the null byte, we'll get there.
the reading stops after:
- an EOF, or
- a newline ๐
and here's the slay: if a newline is read, it gets stored into the buffer. it's not thrown away. it's coming with us. periodt.
then, after the last character in the buffer, a terminating null byte ('\0') is stored. that's your little bow on top ๐
๐ซ RETURN VALUE ๐ซ
- on success,
fgets()returnss. like, it hands you back your own buffer. cute. - when end of file happens and no characters have been read,
NULLis returned. no vibes. empty. ๐ญ - on error,
NULLis returned anderrnois set to tell you what went down.
so if you getNULL, you gotta check: was it EOF or was it an actual error? don't assume, bestie, go look aterrno๐
๐ฅ ERRORS ๐ฅ
see fgetc(3). that's where the error lore lives, we don't duplicate it here, iykyk.
๐ง ATTRIBUTES ๐ง
for what all these terms even mean, see attributes(7). the table said:
fgets()โ Thread safety โ MT-Safe โจ
translation: she's MT-Safe. multiple threads can vibe with her at once, no cap. โญ
๐ STANDARDS ๐
C11, POSIX.1-2008.
๐ฆ HISTORY ๐ฆ
POSIX.1-2001, C89. she's been here a minute, she's a veteran, she ate. ๐
๐ SEE ALSO ๐
and that's the whole page, bestie!! ๐ this was Linux man-pages 6.20, dated 2026-08-19, and yes it's the fgets(3) page, go read your file, go touch some bytes, go be the main character of your own stream ๐ซถโจ๐ธ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, Thomas Koenig <ig25@rz.uni-karlsruhe.de>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms