ungetc()函数ungetc
语法:
1 2 |
?? #include <stdio.h> ?? int ungetc( int ch, FILE *stream ); |
函数ungetc()把字符ch放回到stream(流)中.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include <ctype.h> int main(void) { int ch; int result = 0; printf( "Enter an integer: " ); /* Read in and convert number: */ while( ((ch = getchar()) != EOF) && isdigit( ch ) ) result = result * 10 + ch - '0'; /* Use digit. */ if( ch != EOF ) ungetc( ch, stdin ); /* Put nondigit back. */ printf( "Number = %d\nNextcharacter in stream = '%c'", result, getchar() ); } //输出结果 Output Enter an integer: 521a Number = 521Nextcharacter in stream = 'a' Output Enter an integer: 521 umber = 521Nextcharacter in stream = '' |