[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

opening jpeg file in c++

mohi

10/31/2008 11:21:00 AM

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","wb");
int c;
do{
read(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
9 Answers

Maxim Yegorushkin

10/31/2008 11:36:00 AM

0

On Oct 31, 11:20 am, mohi <mohangupt...@gmail.com> 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 ;
>
> FILE * fp=fopen("./x.jpg","wb");

This opens the file for writing. If a file with the same name already
exists its content is erased and the file is treated as a new empty
file.

--
Max

jt

10/31/2008 12:03:00 PM

0

On Fri, 31 Oct 2008 04:20:45 -0700, 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 ;
>
> FILE * fp=fopen("./x.jpg","wb");
> int c;
> do{
> read(fp,&c,sizeof(c));

error: invalid conversion from â??FILE*â?? to â??intâ??

Note that read (which is not portable, by the way) takes a file
*descriptor*, not a FILE pointer.

[...]

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

A more C++-like style might be something like:

#include <fstream>
#include <iostream>

std::ifstream fs("./x.jpg", std::ios::in|std::ios::binary);
if (!fs) {
// failed to open file - do something about it
}

char c;
while (fs >> c) { // evaluates to false if read fails (e.g. past EOF)
// do something with c
}

fs.close();

--
Lionel B

mohi

10/31/2008 2:58:00 PM

0

On Oct 31, 5:02 pm, Lionel B <m...@privacy.net> wrote:
> On Fri, 31 Oct 2008 04:20:45 -0700, 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 ;
>
> > FILE * fp=fopen("./x.jpg","wb");
> > int c;
> > do{
> > read(fp,&c,sizeof(c));
>
> error: invalid conversion from ‘FILE*’ to ‘int’
>
> Note that read (which is not portable, by the way) takes a file
> *descriptor*, not a FILE pointer.
>
> [...]
>
> > or what is the best way to read a binary file such as an image ??
>
> A more C++-like style might be something like:
>
> #include <fstream>
> #include <iostream>
>
> std::ifstream fs("./x.jpg", std::ios::in|std::ios::binary);
> if (!fs) {
>   // failed to open file - do something about it--
>
> }
>
> char c;
> while (fs >> c) { // evaluates to false if read fails (e.g. past EOF)
>   // do something with c
>
> }
>
> fs.close();
>
> --
> Lionel B

i am really sorry people - i the real code would be 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);

Juha Nieminen

10/31/2008 3:15:00 PM

0

Lionel B wrote:
> char c;
> while (fs >> c) { // evaluates to false if read fails (e.g. past EOF)

Since he wants to read raw binary input into an int, he can do exactly
that also with C++ streams:

fs.read((char*)&theInt, sizeof(int));

He can check if the reading succeeded with fs.fail().

Richard Herring

10/31/2008 3:35:00 PM

0

In message
<13586ffc-5575-4698-a256-3b47c4dfebc8@a17g2000prm.googlegroups.com>,
mohi <mohangupta13@gmail.com> writes

>i am really sorry people - i the real code would be like --
>
>
>FILE * fp=fopen("./x.jpg","rb");
>int c;
>do{
>fread(fp,&c,sizeof(c));

You should be testing here whether the read failed..

>if( c==(int) 0xFF23){

You have a portability problem.
Does the sequence of octets 0xFF, 0x23 correspond to the int value
0xFF23 or 0x23FF?
What happens to your next read from the file if sizeof(c) isn't 2?

>do.....
>do....
>
>}
>
>printf("%x",c);
>}
>
>while(c!=EOF);

Wrong test. This could (coincidentally) happen at any point in the file,
or not at all.

EOF is what fgetc() etc. return. You're calling fread(), which has a
different way of indicating end of file.

--
Richard Herring

jt

10/31/2008 5:20:00 PM

0

On Fri, 31 Oct 2008 15:14:35 +0000, Juha Nieminen wrote:

> Lionel B wrote:
>> char c;
>> while (fs >> c) { // evaluates to false if read fails (e.g. past EOF)
>
> Since he wants to read raw binary input into an int, he can do exactly
> that also with C++ streams:
>
> fs.read((char*)&theInt, sizeof(int));
>
> He can check if the reading succeeded with fs.fail().

What's the advantage of calling read() over extraction here? You can still
check the state of the stream afterwards.

--
Lionel B

James Kanze

10/31/2008 8:01:00 PM

0

On Oct 31, 1:02 pm, Lionel B <m...@privacy.net> wrote:
> On Fri, 31 Oct 2008 04:20:45 -0700, mohi wrote:
> > 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","wb");
> > int c;
> > do{
> > read(fp,&c,sizeof(c));

> error: invalid conversion from ‘FILE*’ to ‘int’

> Note that read (which is not portable, by the way) takes a
> file *descriptor*, not a FILE pointer.

> [...]

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

> A more C++-like style might be something like:

> #include <fstream>
> #include <iostream>

> std::ifstream fs("./x.jpg", std::ios::in|std::ios::binary);

> if (!fs) {
>   // failed to open file - do something about it
> }

> char c;
> while (fs >> c) { // evaluates to false if read fails (e.g. past EOF)

I don't think this is what you want. That's a formatted
extraction; it's going to skip spaces. Which isn't likely to
work if you're reading binary data.

while ( fs.get( c ) ) {

is probably what you're looking for.

>   // do something with c
> }

> fs.close();

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

10/31/2008 8:04:00 PM

0

On Oct 31, 3:58 pm, mohi <mohangupt...@gmail.com> wrote:
> On Oct 31, 5:02 pm, Lionel B <m...@privacy.net> wrote:
> > On Fri, 31 Oct 2008 04:20:45 -0700, mohi wrote:
> > [...]

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

> i am really sorry people - i the real code would be like --

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

Which won't compile either. fread takes 4 arguments (and the
file pointer is the last one).

Also, it's really a worthless function, because it doesn't do
any formatting (or unformatting, as the case may be).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

10/31/2008 8:48:00 PM

0

On Oct 31, 4:14 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Lionel B wrote:
> > char c;
> > while (fs >> c) { // evaluates to false if read fails (e.g. past EOF)

> Since he wants to read raw binary input into an int, he can do
> exactly that also with C++ streams:

> fs.read((char*)&theInt, sizeof(int));

Which doesn't work any better than his proposed solution in C.
The reinterpret_cast should tip you off about that.

> He can check if the reading succeeded with fs.fail().

Or just fs.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34