tags:

views:

562

answers:

5

Hi everyone,

I have many times opened a txt file using C language Files manipulations. But when i try to open an image using C Files, I just cant do that. I even tried this by opening the image file in binary mode("rb").

Could you please help me out for doing this...

Thanks :)

+1  A: 

This really depends on the extension - a BMP is totally different to a TIFF or whatsoever...

You could just get every single pixel within an image and store it. With setpixel() and getpixel() you can store the pixels from the worldview and dmap them to viewport...

Gnark
+2  A: 

Opening files in C is usually (offtopic: how about always? :) done using fopen().

Now, when it comes to reading and assuming you're talking about binary file formats (such as the case with most image file formats), then you have to do some studying first to read in data correctly. If you tell us which file type you're trying to read perhaps you'll get more detailed information.

Michael Foukarakis
Offtopic: If you don't give a damn about portability you might use `open(2)`, so not always. Almost always, though.
Chris Lutz
If you don't give a damn about portability, you might use CreateFile, too. My guess is that many Win32 programs actually use that, so not always, I'd say.
nikie
A: 

This is the code im 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 im directing the image output to DOS window atleast a monochrome pixel image must come up.

Please hint me the necessary changes

identifymecnu
This code will print (hint: printf()) each *char* in the BMP file. It will not draw it.To read a bitmap file in a meaningful way, I suggest you read up on http://en.wikipedia.org/wiki/BMP_file_format, and use a graphics library (plenty of them to go around) to draw pixels on the screen.GL.
Michael Foukarakis
A: 

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.

Tal Pressman
A: 

Hey ... I am Not very Sure about this one .. but i feel that ur file pointer isnt moving ahead .. you need an incremental statement...each time it checks while((ch = fgetc(fp))!=EOF) it comes out true ...!and it goes inside to print out the first pixel .. and repeats!

anjaneai