mohi
10/31/2008 2:58:00 PM
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);