นี่คือตัวอย่างการพิมพ์เนื้อหาของไฟล์ในภาษา 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);
}