所有栏目

ungetc

作者:爱百科

把一个(或多个)字符退回到stream代表的文件流中,可以理解成一个“计数器”。

ungetc介绍

把一个(或多个)字符退回到stream代表的文件流中,可以理解成一个“计数器”。

ungetc用 法

int ungetc(int c, FILE *stream);

ungetc形参

c: 要写入的字符;

stream:文件流指针,必须是输入流不能是输出流

ungetc返回值

字符c - 操作成功,EOF - 操作失败(int)

ungetc程序例

#include <stdio.h>

#include <ctype.h>

int main(void)

{

int ch;

int result = 0;

printf( "Enter an integer: " );

while( ((ch = getchar()) != EOF) && isdigit( ch ) )

result = result * 10 + ch - '0';

if( ch != EOF )

ungetc( ch, stdin );

printf( "Number = %dnNextcharacter in stream = '%c'",

result, getchar() );

}

Output

Enter an integer: 521a

Number = 521Nextcharacter in stream = 'a'

Output

Enter an integer: 521

Number = 521Nextcharacter in stream = '

'

值的一提的是,此程序应该注意换行符的作用。

热点导航
教育资讯 知道问答 公考资讯 司法考试 建筑知识 工作范文 大学排名 报考专业 学习方法 句子美文 秒知回答 作业解答 精选答案 知途问学