C語言,char類型變量不應與EOF直接比較
阿新 • • 發佈:2019-02-04
stdlib.h 使用 using size pac 我們 != getc include
fputc(ch,stdout); //這裏是輸出到屏幕
if(fclose(fp)!=0)
printf("file cannot be closed \n");
else
printf("file is now closed \n");
return 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直接比較