制作宽字符文件 [英] Making wide char file
问题描述
#ifndef UNICODE
#define UNICODE
#endif
#include <stdio.h>
int main()
{
FILE* oFile;
oFile = _wfopen(L"foo.txt",L"w");
//*
fwprintf(oFile,L"%s", L"ęłó☺☻♥♦•ń");
fclose(oFile);
return 0;
}
为什么这个程序创建一个 ASCII 文件而不是 UTF-16,尽管所有函数都是宽的?!
Why this program creates an ASCII file instead of UTF-16, though all functions are wide?!
foo.txt 内容:
foo.txt Content:
za[问号]ó[两个问号...] g[...四个...] ja[另外两个...] [五个*?] [和最后一个]
za[question mark]ó[two question marks...] g[...four...] ja[another two...] [five*?] [and the last one]
这是不可转换的.
fwprintf(oFile,L"%c%c%s",0xFE,0xFF,L"zażółć gęśłą jaźń ☺☻♥♦• ć");
现在,它显示中文符号,无论是小端还是大端字节序标记.
Now, it shows Chinese signs, regardless of little or big endian byte order mark is set.
推荐答案
假设您使用的是 MSVC,引用 _wfopen(我的粗体):
Assuming you are using MSVC, to quote the documentation for _wfopen (bold mine):
fopen 函数打开文件名指定的文件._wfopen 是 fopen 的宽字符版本;_wfopen 的参数是宽字符串.否则,_wfopen 和 fopen 的行为一样.只使用_wfopen对编码字符没有影响文件流中使用的集合.
The fopen function opens the file that is specified by filename. _wfopen is a wide-character version of fopen; the arguments to _wfopen are wide-character strings. Otherwise, _wfopen and fopen behave identically. Just using _wfopen has no effect on the coded character set that is used in the file stream.
进一步阅读文档:
fopen 支持 Unicode 文件流.要打开 Unicode 文件,请传递一个ccs 标志,指定 fopen 所需的编码,如下所示.
fopen supports Unicode file streams. To open a Unicode file, pass a ccs flag that specifies the desired encoding to fopen, as follows.
fopen(&fp, "newfile.txt", "rw, ccs= encoding ");
fopen(&fp, "newfile.txt", "rw, ccs= encoding ");
允许的编码值为 UNICODE、UTF-8 和 UTF-16LE.
Allowed values of encoding are UNICODE, UTF-8, and UTF-16LE.
以下代码写入一个 UTF-8 编码的文件.我将此源文件保存为 UTF-16BE、UTF-16LE 和 UTF-8,并且 MSVC 能够正确编译和运行.
The following code writes a UTF-8-encoded file. I saved this source file in UTF-16BE, UTF-16LE, and UTF-8 and MSVC was able to compile and run correctly.
#include <stdio.h>
int main()
{
FILE* oFile;
oFile = fopen("foo.txt","w, ccs=UTF-8");
fwprintf(oFile,L"%s", L"ęłó☺☻♥♦•ń");
fclose(oFile);
return 0;
}
请注意,在 Windows 命令行上显示此输出需要切换到 UTF-8 代码页:
Note that displaying this output on the Windows command line requires switching to the UTF-8 code page:
C:\x>chcp
Active code page: 1252
C:\x>x
C:\x>type foo.txt
ęłó☺☻♥♦•ń
C:\Users\metolone\Desktop\x>chcp 65001
Active code page: 65001
C:\x>type foo.txt
ęłó☺☻♥♦•ń
这篇关于制作宽字符文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!