我有返回unicode文本的方法,我需要将它们写入文件,但有些字符不会被写入。我有以下内容:
const wchar_t* getStandardText() {
return L"test";
}
const wchar_t* getUnicodeText()
{
return L"testíček";
}
int main()
{
FILE *file = fopen(FILE_NAME, "a");
fputws(getStandardText(), file);
fputws(getUnicodeText(), file);
fclose(file);
}
文件输出:
testtestí
对我来说更令人困惑的是,有些像“í”这样的人物和其他像“č”这样的人没有。
这适用于Windows ...更改您的mode
参数以具有明确的编码...
FILE *file = fopen("foobar.txt", "a+, ccs=UTF-16LE");
要么
FILE *file = fopen("foobar.txt", "a+, ccs=UTF-8");
这似乎强制字节顺序标记(FF FE
)到文件头上,表明文件的文本是Unicode。
必须使用适当的BOM创建文件。以下是最优选的方法,并确保您只将UTF-8字符转储到该文件中。并通过记事本++打开以查看它。
FILE *file = fopen("test.txt", "a+, ccs=UTF-8");