ungetc的用法

来源:互联网 发布:deepin linux 15.4 编辑:程序博客网 时间:2024/05/16 04:52

int  ungetc(char c, FILE *stream)

功能:将一个字符退回到输入流

返回值:c表示操作成功;EOF表示失败。

示例:

#include <iostream>
using namespace std;

#include <ctype.h>


int main(int argc, char* argv[])
{
 int result = 0;
 char ch;
 cout<<"Input an integer: ";
 while((ch = getchar()) != EOF && isdigit(ch))
  result = result * 10 + ch - '0';
 if(ch != EOF)
  ungetc(ch, stdin);
 cout<<result<<" uninteger->"<<(char)(getchar())<<endl;
 return 0;
}

输入:123a

输出:123 uninteger->a

原创粉丝点击