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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值