Thank you very much, it works. I also had to change the line
out = fopen("out.txt","w");
out = fopen("out.txt","wb");

best regards,
Erwin Waterlander

Op 07/20/2009 09:55 AM, Yongwei Wu schreef:
It works if you change this line




And probably you want to change this line

 fprintf (out, "%s\n", wstr);


 fwprintf (out, L"%s\n", wstr);


2009/7/20 Erwin Waterlander <waterlan@xs4all.nl>:

I'm experimenting with UTF-16 unicode in a C program, but I don't have
much experience with the win32 api. I'm more used to UTF-8 on Linux.
What I try to do is to write UTF-16 characters or lines to a file and to
read them back. Has anyone a simple example, for a single character and
a line?

This is my small test program that accepts unicode parameters and prints
them to the console. At the end I try to write a greek delta to a file
and read it back, but this fails.

#include <stdio.h>
#include <wchar.h>
#include <windows.h>
#include <stdarg.h>

// Portable wprintf for Unicode characters.
void wcd_wprintf( const wchar_t* format, ... ) {
   wchar_t wstr[1024];
   va_list args;
#ifdef WIN32
   HANDLE stduit =GetStdHandle(STD_OUTPUT_HANDLE);
   int len;

   va_start( args, format );
#ifdef WIN32
   len = sizeof(wstr);
   vsnwprintf( wstr, len, format, args);
   WriteConsoleW(stduit, wstr, wcslen(wstr), NULL, NULL);
   vwprintf( format, args );
   va_end( args );

// This program can take Unicode arguments on Windows NT
int main (int argc, char ** argv) {

   wchar_t *cmdstr;
   wchar_t **wargv;
   wchar_t *wstr = L"\u0394"; /* greek delta */
   wchar_t wbuf[128];
   wchar_t wc ;
   int i;
   FILE *out;
#ifdef WIN32
   OFSTRUCT *openbuf;

   printf("system cp=%d\n",GetACP());
   printf("console cp=%d\n",GetConsoleOutputCP());

#ifdef WIN32
   // print parameters, including Unicode.
   cmdstr = GetCommandLineW();
   wargv = CommandLineToArgvW(cmdstr, &argc);
   for (i = 0; i < argc; i++) {
       wcd_wprintf(L"wargv[%d]: %s\n", i, wargv[i]);
   for (i = 0; i < argc; i++) {
       printf("argv[%d]: %s\n", i, argv[i]);

   wcd_wprintf(L"greek delta=%s\n",L"\u0394");
   wcd_wprintf(L"pound sign=%s\n",L"\u00a3");
   wcd_wprintf(L"cyrillic Ya=%s\n",L"\u044f");

 // Write unicode to a file.
 out = fopen("out.txt","w");
 fprintf (out, "%s\n", wstr);

 // Read unicode back from file.
 out = fopen("out.txt","r");
 wc = getwc(out);
 wcd_wprintf(L"wide char=%c\n",wc);
 wcd_wprintf(L"wide char=%x\n",wc);

//  HFILE  *wout = OpenFile("out.txt", openbuf, OF_READ);
//  ReadFile(wout, wbuf, 2, NULL, NULL);
//  wbuf[2] = '\0';
//  CloseHandle(wout);
//  wcd_wprintf(L"wide buf=%x\n",wbuf);

 return 0;

Erwin Waterlander

Enter the BlackBerry Developer Challenge
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize
details at: http://p.sf.net/sfu/Challenge
MinGW-users mailing list

This list observes the Etiquette found at
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
You may change your MinGW Account Options or unsubscribe at: