std::fgets(3)
π std::fgets π
okay so bestie, this is std::fgets from the C++ Standard Libary β yes, that spelling is literally on the page, preserving the vibe β¨. Itβs a C++ standard library function, and itβs here to read a line-ish chunk from a file stream into a char array. Buckle up, itβs giving vintage C I/O but make it cute. π
πΈ NAME πΈ
std::fgetsβstd::fgets
π¦ Synopsis π¦
Defined in header <cstdio>:
char* fgets( char* str, int count, std::FILE* stream );
Reads at most count - 1 characters from the given file stream and stores them in the character array pointed to by str. Parsing stops if a newline character is found β in which case str will contain that newline character β or if end-of-file occurs. If bytes are read and no errors occur, writes a null character at the position immediately after the last character written to str. Slay. π
π Parameters π
strβ pointer to an element of achararray.countβ maximum number of characters to write (typically the length ofstr).streamβ file stream to read the data from.
π₯ Return value π₯
str on success, null pointer on failure. Fr fr.
If the end-of-file condition is encountered, sets the eof indicator on stream (see std::feof()). This is only a failure if it causes no bytes to be read, in which case a null pointer is returned and the contents of the array pointed to by str are not altered (i.e. the first byte is not overwritten with a null character). No cap.
If the failure has been caused by some other error, sets the error indicator (see std::ferror()) on stream. The contents of the array pointed to by str are indeterminate (it may not even be null-terminated). π
π§ Notes π§
POSIX additionally requires that fgets sets errno if it encounters a failure other than the end-of-file condition. Bestie, thatβs POSIX doing extra credit. π
Although the standard specification is unclear in the cases where count <= 1, common implementations do:
- if
count < 1, do nothing, report error, - if
count == 1,- some implementations do nothing, report error,
- others read nothing, store zero in
str[0], report success.
π Example π
// Run this code
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <span>
void dump(std::span<const char> buf, std::size_t offset)
{
std::cout << std::dec;
for (char ch : buf)
std::cout << (ch >= ' ' ? ch : '.'), offset--;
std::cout << std::string(offset, ' ') << std::hex
<< std::setfill('0') << std::uppercase;
for (unsigned ch : buf)
std::cout << std::setw(2) << ch << ' ';
std::cout << std::dec << '\n';
}
int main()
{
std::FILE* tmpf = std::tmpfile();
std::fputs("Alan Turing\n", tmpf);
std::fputs("John von Neumann\n", tmpf);
std::fputs("Alonzo Church\n", tmpf);
std::rewind(tmpf);
for (char buf[8]; std::fgets(buf, sizeof buf, tmpf) != nullptr;)
dump(buf, 10);
}
π Output π
Alan Tu. 41 6C 61 6E 20 54 75 00
ring..u. 72 69 6E 67 0A 00 75 00
John vo. 4A 6F 68 6E 20 76 6F 00
n Neuma. 6E 20 4E 65 75 6D 61 00
nn..uma. 6E 6E 0A 00 75 6D 61 00
Alonzo . 41 6C 6F 6E 7A 6F 20 00
Church.. 43 68 75 72 63 68 0A 00
π See also π
scanfβ reads formatted input fromstdin, a file stream or a buffer.fscanfβ (function).sscanfgetsβ reads a character string fromstdin(deprecated in C++11) (function) (removed in C++14).fputsβ writes a character string to a file stream (function).- C documentation for
fgets
xoxo, your fave manual page π β source: http://cppreference.com, version/date: 2024.06.10, page: std::fgets(3). Mwah! π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms