Hello Everyone ,
I have written a simple piece of code for reading an Image file(.png) in c but this code doesn't seem to work properly.Can anyone help me solve this problem of reading an image file in C ? Code I have written is :-

FILE *file;
char ch;
     
file=fopen("img.png","r");     
  
while((ch=fgetc(file))!=EOF )
         printf("%c ",ch);

When running this code , each time I'm getting the same output as
ë P N G
Even if the file size is 2022bytes .Why am I not getting 2022 bytes ? Instead I'm getting only these 4 bytes as output.Please !! help.

A wild guess: you are running on Windows, right?
The png file begins with a signature
ID=89h,'PNG',13,10,26,10
which contains a byte with a decimal value 26, that is ascii Ctrl-Z, which is an end-of-file for a text mode. Open your file with "rb" instead of "r" and enjoy a garbage on your screen.

Yes nezachem , I'm running my code in windows.I've already used "rb" option for opening of file but still the problem remain.If my file size is 30*30 (30*30*3=2700 bytes) , I'm getting only 55 bytes as output , which is not so desirable.I'm trying to do bicubic interpolation of image for which I'm supposed to read each pixel of image for processing. If I'm not able to read pixel value how will I perform operation in it ? Please !! help me in reading pixel values from a image.

Line 2 should declare ch as an int.

Hi Duoas , now code is working well , but is their any explanation why this thing is working with ch declared as int but not with char.
Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.