首页 > 编程语言 > 详细

C语言,char类型变量不应与EOF直接比较

时间:2019-02-03 23:39:51      阅读:277      评论:0      收藏:0      [点我收藏+]

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main()
{
FILE *fp;
char ch;
if((fp=fopen("123.txt","r"))==NULL)
printf("file cannot open \n");
else
printf("file opened for writing \n");
while ( ( ch = fgetc(fp) ) != EOF ) // 注意,这里使用char ch,其实是不科学的,因为最后判断结束标志时,是看ch!=EOF,而EOF的值为-1,这显然和char是不能比较的。所以,某些使用,我们都定义成int ch。
fputc(ch,stdout); //这里是输出到屏幕
if(fclose(fp)!=0)
printf("file cannot be closed \n");
else
printf("file is now closed \n");
return 0;
}

C语言,char类型变量不应与EOF直接比较

原文:https://www.cnblogs.com/bgo-tech/p/10351166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!