Tag: fgetc

fgetc无法识别EOF

下面的程序在各种Solaris / Linux版本上运行良好,但在AIX上运行不正常。 但是,如果我在AIX上用while(c!=0xff)替换while(c!=EOF)它运行完全正常。 有什么想法吗? 我检查了AIX上的fgetc手册页,它应该返回EOF常量! #include #include #include int main() { char c; FILE *fp; fp = fopen(“a.txt”, “r”); c=fgetc(fp); while(c!=EOF) { c=fgetc(fp); printf(“%d”,c); } fclose(fp); return 0; }

比较unsigned char和EOF

当编译以下代码时,它进入无限循环: int main() { unsigned char ch; FILE *fp; fp = fopen(“abc”,”r”); if(fp==NULL) { printf(“Unable to Open”); exit(1); } while((ch = fgetc(fp))!=EOF) printf(“%c”,ch); fclose(fp); printf(“\n”,ch); return 0; } gcc编译器也会在编译时发出警告 abc.c:13:warning: comparison is always true due to limited range of data type 当unsigned char被char或int替换时,代码运行正常,即它终止。 但代码也适用于unsigned int 。 因为我已经读过EOF在stdio.h定义为-1然后为什么这个代码对unsigned char失败但是对unsigned int运行正常。