I have opened a .txt file many times using the C language’s file handling facilities. But when I try to open an image file using the same procedure as for text files, I just can’t do that.
I even tried this by opening the image file in binary mode "rb".
This is the code I’m using:
#include "file.h"
#include "stdio.h"
main()
{
FILE *fp;
char ch;
fp = fopen("D:\\setups\\tcc\\Bluehills.bmp", "rb+");
if(fp == NULL)
{
printf("Error in opening the image");
fclose(fp);
exit(0);
}
printf("Successfully opened the image file");
while((ch = fgetc(fp)) != EOF)
{
printf("%c", ch);
}
printf("\nWriting to o/p completed");
}
What do I need to modify to get the image as it is? As I’m directing the image output to the DOS window at least a monochrome pixel image must come up.
As others have mentioned, since images are binary files you have to know how to “interpret” the data in them after reading them. Luckily, for most formats you can find libraries that do it for you like libpng or libjpeg.