【问题标题】:Why am I getting negative one bytes for my output?为什么我的输出为负一个字节?
【发布时间】: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


【解决方案1】:

您本来打算使用==,但改为使用=

这一行:

if ((file_input = fopen("C:\\Users\\User\\Pictures\\timetable.png", "rb"))=NULL)

应该是:

if ((file_input = fopen("C:\\Users\\User\\Pictures\\timetable.png", "rb"))==NULL)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2021-11-01
    • 1970-01-01
    • 2021-02-20
    • 2016-01-29
    • 2023-04-08
    • 2018-04-11
    相关资源
    最近更新 更多