Home  >  Article  >  Backend Development  >  Usage of fputc function in C language

Usage of fputc function in C language

Guanhui
GuanhuiOriginal
2020-06-19 11:51:126180browse

Usage of fputc function in C language

C语言中fputc函数的用法

C语言中fputc函数的用法为“int fgetc (FILE *fp)”,该函数的作用是从指定的文件中读取一个字符, 读取成功时会返回读取到的字符,读取到文件末尾或读取失败时返回EOF。

推荐教程:《C语言

示例代码

#include<stdio.h>
int main(){
    FILE *fp;
    char ch;
   
    //如果文件不存在,给出提示并退出
    if( (fp=fopen("D:\\demo.txt","rt")) == NULL ){
        puts("Fail to open file!");
        exit(0);
    }

    //每次读取一个字节,直到读取完毕
    while( (ch=fgetc(fp)) != EOF ){
        putchar(ch);
    }
    putchar(&#39;\n&#39;);  //输出换行符

    fclose(fp);
    return 0;
}

推荐文章:《C#教程

The above is the detailed content of Usage of fputc function in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn