C语言读取txt文件

4 提交 / 0个新回复
最新回复
C语言读取txt文件

 

#include <stdio.h>

#include <stdlib.h>//为了使用exit()

#include <conio.h> //为了使用getch()

main()

{

        int ch;

        FILE *fp;

        fp=fopen("d:\\编程\\C\\test.txt","rt");

    if (fp==NULL)

    {

                printf("\nerror on open d:\\编程\\C\\test.txt file");

                getch();

                exit(1);

    }

 

        ch = fgetc(fp);

        while(ch!=EOF)//循环获取直至文件结束 EOF标志(End Of File)

        {

                putchar(ch);  //打印获取到的字符

                ch=fgetc(fp);

        }

                getch();

        fclose(fp); //关闭文件

        return 0;

}