gpt4 book ai didi

c++ - 将 wchar_t* 写入文件 - 仅适用于某些字符

转载 作者:搜寻专家 更新时间:2023-10-31 00:52:17 31 4
gpt4 key购买 nike

我有返回 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 上使用 VS 2015 Pro。
  • 我使用 Notepad++ 读取文件,它告诉我文件有 ANSI 编码。

最佳答案

这适用于 Windows...更改您的 mode 参数以具有显式编码...

FILE *file = fopen("foobar.txt", "a+, ccs=UTF-16LE");

FILE *file = fopen("foobar.txt", "a+, ccs=UTF-8");

这似乎强制将字节顺序标记 (FF FE) 添加到文件头以指示文件的文本是 Unicode。

关于c++ - 将 wchar_t* 写入文件 - 仅适用于某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52253998/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com