什么是ungetc

核心提示把一个(或多个)字符退回到stream代表的文件流中,可以理解成一个“计数器”。中文名ungetc功能把一个(或多个)字符退回定义int ungetc(int ,FILE *);ungetc用 法int ungetc(int c, FILE

最佳答案:

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

详情介绍

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

中文名
ungetc
功能
把一个(或多个)字符退回
定义
int ungetc(int ,FILE *);

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 = '

'

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

 
友情链接
鄂ICP备19019357号-22