다음은 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);
}