What i did wrong ?
Follow MS.
Ok, it's a Joke
From where to begin?
f=fopen(argv[i],"rt,ccs=UNICODE");
PellesC, and standard C, doesn't include any native support for UNICODE. This is an
MS extension. So don't expect that it works under PellesC.
Ansi compliant compilers offer only some support for multibyte, or, more technically correct, UTF-8 UNICODE
encoding.
A common mistake is the tendency to confuse the UNICODE set and the encoding of the UNICODE set itself. Actually UNICODE set (2.0) requires 21 bits to represent the whole characters included in the set, meaning that the plain format, not encoded use 32bits words on the commonly available machines that are based on words being multiples of 8bits.
MS, on the excitation of the first version of UNICODE, was too fast to define WCHAR as 16 bits integer, the result is that the standard wchar cannot represent all the available symbols belonging to the UNICODE set 2.0. I recommend to
read this page to understand the basics.
The standard C runtime function
fgetwc reads from an open stream any UTF-8 encoded UNICODE symbol. The routine read from 1 to 4 bytes following the encoding and give back a symbol expressed in 16bits wchar. For symbols, larger that 16 bits, that cannot be represented in 16 bits, will be used a replacement symbol or will generate an error depending on the function you use.
Using
fgetwc on an UTF-16LE stream, in almost all cases, will give back one byte at time: An ascii value for the first byte of the wchar, and a 0 for the other part. Try to read an UTF-16L file containing symbols having value that requires more than 8 bits and you'll see a really strange behavior from your code
.
How you can handle an UTF-16LE file using an
ANSI compliant compiler?
This way:
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#define BOM 0xFEFF
int main(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++)
{
printf("%s\n", argv[i]);
FILE *fp = fopen(argv[i], "rb");
wint_t c;
//Read BOM
fread((unsigned char *)&c, 2, 1, fp);
if ( c != BOM)
{
return -1;
}
while (fread((unsigned char *)&c, 2, 1, fp))
{
//The following test emulates the filtering of CR in Text files
if (c == '\r') //You can avoid this test if you don't care for CR
continue;
printf("%c", wctob(c));
}
printf("---------> %s\n", strerror(GetLastError()));
fclose(fp);
}
return 0;
}
Your last question:
Same code compiled with gcc gives expected result.
GCC ports for windows uses the MSVCRuntime, so they are
absolutely compliant with ... MS extensions!
Merry Christmas