原函数:int ungetc(int char, FILE *stream),我来为大家讲解一下关于c语言基础函数表?跟着小编一起来看一看吧!

c语言基础函数表(C语言库函数之ungetc详解)

c语言基础函数表

原函数:

int ungetc(int char, FILE *stream)

函数说明: int ungetc(int char, FILE *stream) 将字符的字符(unsigned char类型)到指定的流,用于下一个读操作。

参数:

返回值:

如果成功,则返回字符推回,否则,返回EOF并流保持不变。

如何使用ungetc() 函数:

#include <stdio.h>

int main () {

FILE *fp;

int c;

char buffer [256];

fp = fopen("file1.txt", "r");

if( fp == NULL ) {

perror("Error in opening file \n");

return -1;

}

while(!feof(fp)) {

c = getc (fp); /* replace ! with */

if( c == '!' ) {

ungetc (' ', fp);

} else {

ungetc(c, fp);

}

fgets(buffer, 255, fp);

fputs(buffer, stdout);

}

return 0;

}

假设有一个文本文件file1.txt,其中包含以下数据。

this is myfoal !c standard library !library functions and macros

编译和运行上面的程序,产生如下结果:

this is myfoal

c standard library

library functions and macros

library functions and macros

,