ungetc
目录
展开
展开
把一个字符退回到输入流中
int ungetc(char c, FILE *stream);
c 要写入的字符,stream文件流指针
字符c - 操作成功,EOF- 操作失败
#include <stdio.h>
#include <ctype.h>
void 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 nondigitback. */
printf( "Number =%d\nNextcharacter in stream = '%c'",
result, getchar() );
}
Output
Enter an integer: 521a
Number = 521Nextcharacter in stream ='a'