8.3 打开一个文件并显示该文件

8.3 file_eof.c -- 打开一个文件并显示该文件

#include <stdio.h>
#include <stdlib.h>           // 为了使用exit()
int main()
{
    int ch;
    FILE *fp;
    char fname[50];           // 存储文件名

    printf("Enter the name of the file: ");
    scanf("%s", fname);
    fp = fopen(fname, "r");       // 打开待读取文件
    if (fp == NULL)
    {
        printf("Failed to open file. Bye\n");
        exit(1);                  // 推出程序
    }

    // getc(fp) 从打开的文件中获取一个字符
    while ((ch = getc(fp)) != EOF)
        putchar(ch);

    fclose(fp);                  // 关闭文件

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/EisNULL/p/10828904.html
8.3