简体   繁体   中英

Write wide-character to stream

I would like to write a wide-character to a file in C.

Here is what I do but it doesn't seem to work...the file remains empty.

wchar_t c = 0x2592;
FILE *file;
file = fopen("foo.txt","w");
fputwc(c, file);

Of course I put the setlocale(LC_ALL, "fr_FR.utf8"); in the main function.

Change setlocale parameters, after I've changed it it worked for me:

setlocale(LC_CTYPE, "fr_FR");

EDIT: The above I tested on Mac OS X. It seemed to work, the output file had 3 bytes.

Then when I tried the same on a Linux box (OpenSuse 11) it worked different: the result file had only one byte... So I changed it back to setlocale(LC_ALL, "fr_FR.utf8"); and it worked seemingly OK, the output file had 3 bytes... So it seems like it's a bit system dependant, the functions are POSIX, but I think how they work depends on what languages you have installed or something like that... All the functions return error codes, you should check them. Read the manual of each function and see what error codes they can return.

您需要刷新输出以实际写入字符:

fflush(file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM