Reputation: 101
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.
Upvotes: 0
Views: 150
Reputation: 26184
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.
Upvotes: 1
Reputation: 943
you need to flush the output for the character to be actually written:
fflush(file);
Upvotes: 0