1

I am trying to read the bytes of an image file in C, specifically a PNG, in the following manner:

#include <stdio.h>

int main(){
  FILE* fd = fopen("logo.png", "r");
  char c = fgetc(fd);
  while(c != EOF){
    printf("%c", c);
    c = fgetc(fd);
  }
  return 0;
}

When I run the program, I get he following:

<89>PNG^M
^Z
^@^@^@^MIHDR^@^@^@

Why does it only go to a certain byte and then completely exit reading the file?How would I fix this issue?

user3864563
  • 365
  • 1
  • 7
  • 22

1 Answers1

4

The code contains an error.

// Wrong
char c = fgetc(fd);

The correct version is:

// Correct
int c = fgetc(fd);

What is happening is the byte 0xff appears in your PNG file somewhere. So you get:

char c = 0xff;

But that's an overflow, and in this case it happens to result in:

char c = -1;

And EOF is also -1.

(Note that if you read the C standard it turns out that we've actually encountered undefined behavior, but a discussion of undefined behavior can turn into a rabbit hole.)

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    I like the last comment :D – P0W Jan 27 '17 at 05:41
  • Thank you! This fixed a bug that was in my program. The character that my code was getting stuck on was the \377. I knew the character was having the issue, but what was happening was confusing me. – user3864563 Jan 27 '17 at 05:53