Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      ungetc

      From cppreference.com
      <c‎ |io
       
       
      File input/output
      Types and objects
      Functions
      File access
      Unformatted input/output
      (C95)(C95)
      (C95)
      (C95)(C95)
      (C95)
      (C95)

      Formatted input
       
      Defined in header<stdio.h>
      int ungetc(int ch,FILE* stream);

      Ifch does not equalEOF, pushes the characterch (reinterpreted asunsignedchar) into the input buffer associated with the streamstream in such a manner that subsequent read operation fromstream will retrieve that character. The external device associated with the stream is not modified.

      Stream repositioning operationsfseek,fsetpos, andrewind 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 flagfeof.

      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

      [edit]Parameters

      ch - character to be pushed into the input stream buffer
      stream - file stream to put the character back to

      [edit]Return value

      On successch is returned.

      On failureEOF is returned and the given stream remains unchanged.

      [edit]Notes

      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).

      [edit]Example

      Demonstrates the original purpose ofungetc: implementation ofscanf

      Run this code
      #include <ctype.h>#include <stdio.h> void demo_scanf(constchar* fmt,FILE* s){while(*fmt!='\0'){if(*fmt=='%'){int c;switch(*++fmt){case'u':while(isspace(c=getc(s))){}unsignedint num=0;while(isdigit(c)){                        num= num*10+ c-'0';                        c=getc(s);}printf("%%u scanned %u\n", num);                    ungetc(c, s);break;case'c':                    c=getc(s);printf("%%c scanned '%c'\n", c);break;}}else++fmt;}} int main(void){FILE* f=fopen("input.txt","w+");if(f!=NULL){fputs("123x", f);rewind(f);        demo_scanf("%u%c", f);fclose(f);}return0;}

      Output:

      %u scanned 123%c scanned 'x'

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.21.7.10 The ungetc function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.21.7.10 The ungetc function (p: 243)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.21.7.10 The ungetc function (p: 334)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.19.7.11 The ungetc function (p: 300)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.9.7.11 The ungetc function

      [edit]See also

      gets a character from a file stream
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/io/ungetc&oldid=172393"

      [8]ページ先頭

      ©2009-2025 Movatter.jp