用 C 打印文件内容


以下是使用 C 语言打印文件内容的一个示例,

假设我们有一个内容如下所示的 "new.txt" 文件。

0,hell!o
1,hello!
2,gfdtrhtrhrt
3,demo

现在,让我们来看一下这个示例。

示例

#include<stdio.h>
#include<conio.h>
void main() {
   FILE *f;
   char s;
   clrscr();
   f=fopen("new.txt","r");
   while((s=fgetc(f))!=EOF) {
      printf("%c",s);
   }
   fclose(f);
   getch();
}

输出

0,hell!o
1,hello!
2,gfdtrhtrhrt
3,demo

在上面的程序中,我们有一个文本文件 "new.txt"。文件指针用于打开和读取文件。它显示文件的内容。

FILE *f;
char s;
clrscr();
f=fopen("new.txt","r");
while((s=fgetc(f))!=EOF) {
   printf("%c",s);
}

更新日期:2020 年 6 月 26 日

6K+ 浏览量

开启 事业

通过完成课程获得认证

开始
广告
© . All rights reserved.