文件操作函数feof判断是否指向文件的结尾处,结尾即为空字符

来源:互联网 发布:梦幻辅助软件 编辑:程序博客网 时间:2024/05/14 01:11
#include <STDIO.H>#include <conio.h>int main(){FILE *fStream = NULL;intiReturn = 1;char ch;fStream = fopen("d:\\wimrepairread.txt","r+");if (fStream != NULL){fseek(fStream,0,SEEK_SET);ch = fgetc(fStream);if (feof(fStream)){printf("It reaches the end\n");}else{printf("It doesn't reach the end\n");}//feof若当前文件指针已经移动到结尾,则返回非0。若未移动到结尾,返回0.//可以使用fgetc得到的值是否为EOF来替换。if (ch == EOF)//EOF是文本文件结束的标志。{printf("It reaches the end\n");}else{printf("It doesn't reach the end\n");}while(!feof(fStream)){ch = fgetc(fStream);printf("%c\n", ch); //当feof返回值为非0时,fgetc获得的值为空。}iReturn = 0;goto exit;}else{iReturn = 1;goto exit;}exit:if (fStream!=NULL){fclose(fStream);fStream = NULL;}return iReturn ;}

开始时,不要难过忘记:FILE *fStream = NULL;
结束之后,千万不要忘记以下这段代码:
exit:if (fStream!=NULL){fclose(fStream);fStream = NULL;}

0 0
原创粉丝点击