Writing to a file in Unicode

I am having problems writing a Unicode file inside my program. I am trying to write a japanese unicode string to a file. When I go to check the file, although it is empty. If I try a non-unicode string, it works fine. What am I doing wrong?

setlocale(LC_CTYPE, ""); FILE* f; f = _wfopen(COMMON_FILE_PATH,L"w"); fwprintf(f,L"日本語"); fclose(f); 

About my system: I am running Windows. And my IDE is Visual Studio 2008.

+6
c unicode
source share
3 answers

You may need to add encoding to mode. Perhaps it:

 f = _wfopen(COMMON_FILE_PATH,L"w, ccs=UTF-16LE"); 
+8
source share

Performing the same action with fopen() works for me here. I use Mac OS X, so I don't have _wfopen() ; if _wfopen() doesn't return bad things to you, your code should work.

Edit: I also tested on cygwin - it works fine too.

0
source share

I can’t find the link to _wfopen in any of my fields, however I don’t see why opening it with fopen should cause a problem, all you need is a file pointer.

The important thing is that whether or not C recognizes the internal Unicode values ​​and accordingly pushes these binary values ​​into the file.

Try using fopen, as Karl suggested, it should work correctly.

Edit: if it still does not work, you can try to define the characters as their integer values ​​and push them with fwprintf (), I know that this is cumbersome and not a good solution in the long run, but it should work as well.

0
source share

All Articles