Alex
Alex

Reputation: 7

Reading a file with fgetc stops unexpectedly

I'm trying to make a program that, given an input image "test.jpg", create a copied image "copy.jpg" in the same folder.

I already written that:

int main()
{
    FILE *file;
    file = fopen("test.jpg", "rb");
    unsigned char d;
    while((d = fgetc(file)) != EOF){
        printf("%u ", d);
    }
    return 0;
}

But it goes in loop printing only 255.

Upvotes: 0

Views: 84

Answers (1)

Arkku
Arkku

Reputation: 42129

The value of EOF does not fit in an unsigned char (otherwise reading a character of that value could not be distinguished from the end of file). You need to have int d.

Upvotes: 3

Related Questions