【发布时间】:2013-02-07 06:09:57
【问题描述】:
我学习 C 语言。我需要用 Unicode 数据创建一些文本文件。我写过这样的代码:
#include<stdio.h>
#include<stdlib.h>
#include<wchar.h>
int main(int argc, char *argv[]) {
wchar_t *s1 = L"Привет, мир!\n";
wchar_t *s2 = L"Hello, World!";
FILE *fp = fopen("./hello.txt", "w");
fputws(s1, fp);
fputws(s2, fp);
fclose(fp);
exit(EXIT_SUCCESS);
}
但我得到这样的结果:
??????, ???!
Hello, World!
为什么我没有得到俄罗斯字符?
【问题讨论】:
-
请说明您是如何查看文件的
-
我在 gedit (Linux) 中打开它。
-
gedit 认为文件是什么编码?它与实际编码匹配吗?如果您使用的是 Linux,那么我怀疑 UTF-8 将是首选编码。
-
当我按“另存为”时,我会看到“Utf-8”。
-
如果您想要 UTF-8,请使用 UTF-8 编写代码并使用纯字符。它会起作用的。将 wchar_t 用于“Unicode”只是 Microsoft 的实现。