//endOfFile.c -- use feof()function check the end of file. #include <stdio.h> #include <stdlib.h> #define BUFSIZE 100 int main(void){ char buf[BUFSIZE]; char filename[60]; FILE *fp; puts("Enter name of text file to display: "); gets(filename); // open file by r mode if((fp = fopen(filename, "r")) == NULL){ fprintf(stderr, "Error opening file."); exit(1); } // 若是未達到文件末尾, 讀取一行並顯示 while(!feof(fp)){ fgets(buf, BUFSIZE, fp); printf("%s", buf); } fclose(fp); return 0; }