Re: opening jpeg file in c++



mohi wrote:
hello everyone ,

i am trying to read a jpeg image through c++ but is unable to do so ,
presently i tried it with code in c as i use something like ;

Sorry; I can only debug what I can see. If the problems I
notice in what you've shown are only "something like" what's
happening in your actual code, you have only yourself to blame
for the imperfect approximation.

FILE * fp=fopen("./x.jpg","rb");

And how do you know that the fopen() succeeded, hmmm?

int c;
do{
fread(fp,&c,sizeof(c));

There are at least three problems here. First, fread() requires
four arguments but you have supplied only three; the compiler should
have given you an error message here.

Second, it appears that you are trying to read two bytes from the
file, but sizeof(c) might not be equal to two. On most general-purpose
machines these days the value will be four; the value one is found on
some special-purpose hardware. Two was once common, but has become
rare. If you want to read two bytes, say `2'.

Third, there's the problem of representation, usually in the
form of "endianness." Even if sizeof(c) is two, setting c's first
byte to 0xFF and second byte to 0x23 might not produce the value
0xFF23[*], but might give you 0x23FF instead. If the "endianness"
of your machine is different from the "endianness" of JPEG, the
dump-the-bytes-into-the-int-and-hope approach will not work. See
the FAQ for more on this matter.

[*] In fact, a sixteen-bit `int' cannot *ever* equal 0xFF23,
which is a number too large for the variable's range.

if( c==(int) 0xFF23){
do.....
do....

}

printf("%x",c);

"%x" converts an `unsigned int' argument, but you supply a
plain (signed) `int'. Not good. Also, since you never put any
space between the output of successive calls, you will have a
hard time distinguishing one piece of output from the next. And
finally, since you never output a newline character to end the
line, there's no guarantee you'll ever see any output at all.


}

while(c!=EOF);

Here's another problem: fread() does not report end-of-file
by storing EOF in the data area, but by returning the number of
elements it was able to read as the value of the fread() function.
You have learned, it seems, that red means stop and green means go,
but instead of looking at the traffic signal you are looking at
the color of your hood ornament.

I'll suggest it again: Read the FAQ. http://www.c-faq.com/

--
Eric.Sosman@xxxxxxx
.