I am having some problems writing to a file in unicode inside my c program. I am trying to write a unicode Japanese string to a file. When I go to check the file though it is empty. If I try a non-unicode string it works just fine. What am I doing wrong?
setlocale(LC_CTYPE, "");
FILE* f;
f = _wfopen(COMMON_FILE_PATH,L"w");
fwprintf(f,L"日本語");
fclose(f);
Oh about my system: I am running Windows. And my IDE is Visual Studio 2008.
-
Doing the same with
fopen()
works for me here. I'm using Mac OS X, so I don't have_wfopen()
; assuming_wfopen()
isn't returning bad stuff to you, your code should work.Edit: I tested on cygwin, too - it also seems to work fine.
Lefteris : Didn't work for me :( , _wfopen returns a normal FILE* pointer. I am running Windows. And my IDE is Visual Studio 2008. -
I cannot find a reference to _wfopen on either of my boxes, I however don't see why opening it with fopen should cause a problem, all you need is a file pointer.
What matters is if or not C recognizes the internal Unicode's values and pushes those binary values to the file properly.
Try just using fopen as Carl suggested, it should work properly.
Edit: if it still doesn't work you may try defining the characters as their integer values and pushing them with fwprintf(), I know that's cumbersome and not a good fix in the long run, but it should work as well.
-
You might need to add the encoding to the mode. Possibly this:
f = _wfopen(COMMON_FILE_PATH,L"w, ccs=UTF-16LE");
Lefteris : Thanks .. this worked. But that creates an additional question. I actually wanted this so that I could be able to write unicode to files generally. Japanese was just an example. This will work for all unicode supported languages, correct?Mark Wilkins : Correct. It should not be language dependent.
0 comments:
Post a Comment