ungetc()函数的用法

ungetc函数是将输出流中的废弃数据退入流中去。

MSDN是这样定义的

int ungetc(
   int c,
      FILE *stream 
);
Parameters
c Character to be pushed.
stream Pointer to FILE structure.
Return Value
If successful, each of these functions returns the character argument c. If c cannot be pushed back or if no character has been read, the input stream is unchanged and ungetc returns EOF; ungetwc returns WEOF. If stream is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue,EOF or WEOF is returned and errno is set to EINVAL.


贴出实例代码:代码通过VS2008编译
/*************************************************************/
#include <iostream>
//#pragma  pack(4)
#include <cctype>




int main(int argc,char *argv[])
{
    int ch=0,sum=0;
    while ( ( ch=getchar() )!=EOF&&isdigit(ch) )
    {
        sum*=10;
        ch-='0';
        sum+=ch;
    }
    ungetc(ch,stdin);
    printf("%d\n",sum);
    fflush(stdin);
    system("pause");
    return 0;
}
/*******************************************************************/
输入:12345r
输出:12345

 
 

你可能感兴趣的:(ungetc)