opening jpeg file in c++

M

mohi

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 ;

FILE * fp=fopen("./x.jpg","rb");
int c;
do{
fread(fp,&c,sizeof(c));
if( c==(int) 0xFF23){
do.....
do....

}

printf("%x",c);

}

while(c!=EOF);



but the problem is the if condition never evalutes to true as i know
that according to the jpeg standard there should be market with value
0xFFD8 and others also .....and also the printf() of integer 'c' as
hex is never displayed it just displays a blank ..

what could be wrong ??

or what is the best way to read a binary file such as an image ??

thanks a lot
mohan gupta
 
D

David Resnick

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 ;

FILE * fp=fopen("./x.jpg","rb");
int c;
do{
fread(fp,&c,sizeof(c));
if( c==(int) 0xFF23){
do.....
do....

}

printf("%x",c);

}

while(c!=EOF);

but the problem is the if condition never evalutes to true as i know
that according to the jpeg standard there should be market with value
0xFFD8 and others also .....and also the printf() of integer 'c' as
hex is never displayed it just displays a blank ..

Make sure you turn on compiler warnings and #include <stdio.h>.
Look up fread, your code above has 3 arguments, fread has 4 and in a
sort of strange order:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE
*stream);

0xFF23 is very likely bigger than an int on your machine, btw.

c will not be set to EOF, look at return status of fread.

You have lots of other issues too, but that should get you started.

-David
 
J

jameskuyper

mohi said:
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 ;

FILE * fp=fopen("./x.jpg","rb");
int c;
do{
fread(fp,&c,sizeof(c));

You don't have enough arguments here, and they're in the wrong order.
I think that should be

if(fread(&c, sizeof(c), 1, fp) != 1)
{
// Error handling.
}

You're assuming here that the value you're looking for is stored in
sizeof(int) bytes. That's not a portable assumption - are you sure
it's correct?
if( c==(int) 0xFF23){

If INT_MAX < 0xFF23, that conversion has undefined behavior.
do.....
do....

}

printf("%x",c);

}

while(c!=EOF);



but the problem is the if condition never evalutes to true as i know
that according to the jpeg standard there should be market with value
0xFFD8 and others also .....and also the printf() of integer 'c' as

Well, your code tests against 0xFF23, not 0xFFD8. Could that be the
problem?
Also, are you sure that the endianess of the data in the file that
you're reading is the same as the endianess of 'int' on the compiler
you're using?
hex is never displayed it just displays a blank ..

what could be wrong ??

or what is the best way to read a binary file such as an image ??

Assuming CHAR_BIT==8 and that the jpeg tag is stored in 16 bits, in
big-endian order, the best way to handle this is:

#include <limits.h>
#if CHAR_BIT != 8
#error This code assumes CHAR_BIT == 8
#endif
unsigned char buffer[2];
if(fread(buffer, 2, 1, fp)!=1)
{
// Error handling
}
else
{
if(buffer[0] == 0xFF && buffer[1] == 0x23 /* ? 0xD8 ? */)
// handle as jpeg file
}
else
{
// handle as non-jpeg file.
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top