| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Types and objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <cstdio> | ||
char* fgets(char* str,int count,std::FILE* stream); | ||
Reads at mostcount-1 characters from the given file stream and stores them in the character array pointed to bystr. Parsing stops if a newline character is found, in which casestr 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 tostr.
Contents |
| str | - | pointer to an element of a char array |
| count | - | maximum number of characters to write (typically the length ofstr) |
| stream | - | file stream to read the data from |
str on success, null pointer on failure.
If the end-of-file condition is encountered, sets theeof indicator onstream (seestd::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 bystr are not altered (i.e. the first byte is not overwritten with a null character).
If the failure has been caused by some other error, sets theerror indicator (seestd::ferror()) onstream. The contents of the array pointed to bystr are indeterminate (it may not even be null-terminated).
POSIX additionally requires thatfgets setserrno if it encounters a failure other than the end-of-file condition.
Although the standard specification isunclear in the cases wherecount<=1, common implementations do
#include <cstdio>#include <cstdlib>#include <iomanip>#include <iostream>#include <span> void dump(std::span<constchar> 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
| reads formatted input fromstdin, a file stream or a buffer (function)[edit] | |
(deprecated in C++11)(removed in C++14) | reads a character string fromstdin (function)[edit] |
| writes a character string to a file stream (function)[edit] | |
C documentation forfgets | |