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

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

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;
}spa

相关文章
相关标签/搜索