fwprintf省略宽字符 [英] fwprintf omits wide chars

查看:136
本文介绍了fwprintf省略宽字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows上使用MinGW的C创建宽字符文件,但宽字符似乎被忽略。我的code:

I'm trying to create wide chars file using MinGW C on Windows, however wide chars seem to be omitted. My code:

const wchar_t* str = L"příšerně žluťoučký kůň úpěl ďábelské ódy";
FILE* fd = fopen("file.txt","w");
// FILE* fd = _wfopen(L"demo.txgs",L"w"); // attempt to open wide file doesn't help
fwide(fd,1); // attempt to force wide mode, doesn't help
fwprintf(fd,L"%ls",str);
// fputws(p,fd); // stops output after writing "p" (1B file size)
fclose(fd);

文件内容

píern luouký k úpl ábelské ódy

文件大小为30B,所以宽字符真的错过。如何说服编译器将它们写?

The file size is 30B, so the wide chars are really missing. How to convince the compiler to write them?

由于@chqrlie建议在评论中:结果

As @chqrlie suggests in the comments: the result of

fwrite(str, 1, sizeof(L"příšerně žluťoučký kůň úpěl ďábelské ódy"), fd);

82(我猜2 * 30 + 2 * 10(ommited字符)+ 2(宽尾随零))。

is 82 (I guess 2*30 + 2*10 (ommited chars) + 2 (wide trailing zero)).

这也可能是有用的报价从这里

It also might be useful to quote from here

在文件中宽字符的外部重新presentation多字节
  人物:这些获得仿佛wcrtomb被称为转换
  每个宽字符(使用流的内部的mbstate_t对象)。

The external representation of wide characters in files are multibyte characters: These are obtained as if wcrtomb was called to convert each wide character (using the stream's internal mbstate_t object).

这解释了为什么ISO-8859-1字符是文件中的单字节,但我不知道如何使用这些信息来解决我的问题。做相反的任务(读取多字节UTF-8成宽字符)我没有使用 mbtowc 并结束了使用WINAPI的的的MultiByteToWideChar

Which explains why the ISO-8859-1 chars are single byte in the file, but I don't know how to use this information to solve my problem. Doing the opposite task (reading multibyte UTF-8 into wide chars) I failed to use mbtowc and ended up using winAPI's MultiByteToWideChar.

推荐答案

我不是一个Windows用户,但你可以试试这个:

I am not a Windows user, but you might try this:

const wchar_t *str = L"příšerně žluťoučký kůň úpěl ďábelské ódy";
FILE *fd = fopen("file.txt", "w,ccs=UTF-8");
fwprintf(fd, L"%ls", str);
fclose(fd);

我得到了这个问题,这样的想法:<一href=\"http://stackoverflow.com/questions/3973582/how-do-i-write-a-utf-8-en$c$cd-string-to-a-file-in-windows-in-c\">How做我写一个UTF-8 EN codeD字符串Windows中的文件,在C ++

这篇关于fwprintf省略宽字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆