Usage of fputc function in C language

Guanhui
Release: 2020-06-19 11:51:12
Original
6243 people have browsed it

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;
}
Copy after login

推荐文章:《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!

Related labels:
c
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template