读取文件的内容

106 阅读1分钟

   

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp;//文件指针
    char ch;
    if((fp = fopen("F:\\Codes\\Codeblocks\\C\\FileReading\\simple.txt","r"))==NULL)//文件的路径
    {
        printf("Can not open this file!\n");//找不到文件时结束
        exit(0);
    }
    ch = fgetc(fp);//获取文件中的字符
    while(!feof(fp))//判断是否到文件末尾
    {
        putchar(ch);//输出字符
        ch = fgetc(fp);
    }
    printf("\n");
    fclose(fp);//关闭文件
    return 0;
}