【发布时间】:2021-05-10 04:18:47
【问题描述】:
我尝试使用 C 为“png”文件的可视化引擎开发一个简单的元数据可视化工具,但我的输出得到 -1,我不知道为什么。谁能告诉我错误在哪里?
#include
int main ()
{
FILE *file_input;
unsigned char c; //unsigned
long int counter=0L;
long int saizFail, curPos=0L, current=0L;
if ((file_input = fopen("C:\\Users\\User\\Pictures\\timetable.png", "rb"))=NULL)
printf("File cannot be opened");
fseek(file_input, 0L, SEEK_END); //
saizFail=ftell(file_input);
rewind(file_input);
printf("File size = %ld bytes\n\n", saizFail);
while (counter<100)
{
c=fgetc(file_input);
printf("counter %ld >> %X\n", counter, c);
counter++;
}
printf(">>File size = %ld bytes\n\n", saizFail);
fclose(file_input);
getchar();
}
【问题讨论】:
-
fgetc返回int,而不是unsigned char,并且您应该在将其用作字符之前检查返回值是否不等于EOF。事实上fgetc失败了,因为selbie解释的原因,所以fgetc返回EOF表示失败,但是你的代码没有注意并输出为unsigned char无论如何,这解释了-1.
标签: c compiler-errors output metadata