【发布时间】:2015-10-17 19:58:43
【问题描述】:
我是 C 和文件处理的新手,我正在尝试打印文件的内容。如果这很重要,我正在使用 Code::Blocks。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char c;
FILE *f;
f = fopen("filename.txt", "rt");
while((c=fgetc(f))!=EOF){
printf("%c", c);
}
fclose(f);
return 0;
}
【问题讨论】:
-
好的,有什么问题?
-
if (f==NULL) ... // error handling -
将
fgetc的结果分配给char是错误的。EOF不是char。 -
请将
char c;更改为int c;,因为这是fgetc()返回的类型。值EOF必须与数据值0xFF区分开来。使用printf("%c", c);仍然是完全可以接受的,因为用作printf的参数的char无论如何都会提升为int。 -
@WeatherVane 我会试试的。谢谢。
标签: c file file-handling