From: Jean on
Hello

this code works:

unsigned char buffer[1025];
fopen("toto.bmp", "rb");
fread(buffer, sizeof(unsigned char),1024, pf);
fclose(pf);

if(buffer[0] == 'B' && buffer[1] == 'M')
...

this code does not work: (compiled with UNICODE and _UNICODE)

WCHAR buffer[1025];
_wfopen(L"toto.bmp", L"rb");
fread(buffer,sizeof(WCHAR),1024,pf);
fclose(pf);

if(buffer[0] == 'B' && buffer[1] == 'M')
...

it's the comparison that does not work.
(i tried if(buffer[0] == L'B' && buffer[1] == L'M') too)
any idea ?

jean


From: Xavier Roche on
Jean a écrit :
> this code does not work: (compiled with UNICODE and _UNICODE)

You are incorrectly assuming that fread() is unicode-aware. But it's
not: fread() reads _bytes_, not characters, and your example will not
work unless the file is UCS-2 (little endian) encoded.

The only impact of unicode is on the (w)fopen side: once the file is
opened, fread will behave strictly identically.

By the way, defining UNICODE is not necessary in your case, as you are
using explicit UCS2 functions such as _wfopen ; UNICODE would have an
impact on _tfopen and friends only.

[ And AFAICS, you are processing a binary BMP file - you should stick to
unsigned char buffers ]
From: ScottMcP [MVP] on
This is comparing unicode data with ANSI characters:

if(buffer[0] == 'B' && buffer[1] == 'M')

Try it this way:

if(buffer[0] == L'B' && buffer[1] == L'M')
From: Angus on
On 25 Apr, 16:56, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote:
> This is comparing unicode data with ANSI characters:
>
> if(buffer[0] == 'B'  && buffer[1] == 'M')
>
> Try it this way:
>
> if(buffer[0] == L'B'  && buffer[1] == L'M')

Or if you use C++ you could use the wfstream.
From: Jean on
>And AFAICS, you are processing a binary BMP file - you should stick to
>unsigned char buffers
it was an example, my program works with any file type

Jean
"Xavier Roche" <xroche(a)free.fr.NOSPAM.invalid> a �crit dans le message de
news: hr1kjn$14m$1(a)news.httrack.net...
> Jean a �crit :
>> this code does not work: (compiled with UNICODE and _UNICODE)
>
> You are incorrectly assuming that fread() is unicode-aware. But it's not:
> fread() reads _bytes_, not characters, and your example will not work
> unless the file is UCS-2 (little endian) encoded.
>
> The only impact of unicode is on the (w)fopen side: once the file is
> opened, fread will behave strictly identically.
>
> By the way, defining UNICODE is not necessary in your case, as you are
> using explicit UCS2 functions such as _wfopen ; UNICODE would have an
> impact on _tfopen and friends only.
>
> [ And AFAICS, you are processing a binary BMP file - you should stick to
> unsigned char buffers ]