| 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> | ||
int ungetc(int ch,std::FILE*stream); | ||
Ifch does not equalEOF, pushes the characterch (reinterpreted asunsignedchar) into the input buffer associated with the streamstream in such a manner than subsequent read operation fromstream will retrieve that character. The external device associated with the stream is not modified.
Stream repositioning operationsstd::fseek,std::fsetpos, andstd::rewind discard the effects ofungetc.
Ifungetc is called more than once without an intervening read or repositioning, it may fail (in other words, a pushback buffer of size 1 is guaranteed, but any larger buffer is implementation-defined). If multiple successfulungetc were performed, read operations retrieve the pushed-back characters in reverse order ofungetc
Ifch equalsEOF, the operation fails and the stream is not affected.
A successful call toungetc clears the end of file status flagstd::feof.
A successful call toungetc on a binary stream decrements the stream position indicator by one (the behavior is indeterminate if the stream position indicator was zero).
A successful call toungetc on a text stream modifies the stream position indicator in unspecified manner but guarantees that after all pushed-back characters are retrieved with a read operation, the stream position indicator is equal to its value beforeungetc.
Contents |
| ch | - | character to be pushed into the input stream buffer |
| stream | - | file stream to put the character back to |
On successch is returned.
On failureEOF is returned and the given stream remains unchanged.
The size of the pushback buffer varies in practice from 4k (Linux, MacOS) to as little as 4 (Solaris) or the guaranteed minimum 1 (HPUX, AIX).
The apparent size of the pushback buffer may be larger if the character that is pushed back equals the character existing at that location in the external character sequence (the implementation may simply decrement the read file position indicator and avoid maintaining a pushback buffer).
demonstrates the use ofstd::ungetc in its original purpose: implementingstd::scanf
#include <cctype>#include <cstdio> void demo_scanf(constchar* fmt,std::FILE* s){while(*fmt!='\0'){if(*fmt=='%'){switch(*++fmt){case'u':{int c{};while(std::isspace(c=std::getc(s))){}unsignedint num{};while(std::isdigit(c)){ num= num*10+ c-'0'; c=std::getc(s);}std::printf("%%u scanned %u\n", num); std::ungetc(c, s);break;}case'c':{int c=std::getc(s);std::printf("%%c scanned '%c'\n", c);break;}}}else{++fmt;}}} int main(){if(std::FILE* f=std::fopen("input.txt","w+")){std::fputs("123x", f);std::rewind(f); demo_scanf("%u%c", f);std::fclose(f);}}
Output:
%u scanned 123%c scanned 'x'
| gets a character from a file stream (function)[edit] | |
C documentation forungetc | |