问题描述
我已经阅读了一堆文章和论坛的帖子讨论这个问题所有的解决方案似乎太复杂这样一个简单的任务。
I've read a bunch of articles and forums posts discussing this problem all of the solutions seem way too complicated for such a simple task.
这里是一个示例代码直接从cplusplus.com:
Here's a sample code straight from cplusplus.com:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
只要example.txt只有ASCII字符,
It works fine as long as example.txt has only ASCII characters. Things get messy if I try to add, say, something in Russian.
在GNU / Linux中,将文件保存为UTF-8非常简单。
In GNU/Linux it's as simple as saving the file as UTF-8.
在Windows中,这不工作。将文件转换为UCS-2 Little Endian(默认情况下,Windows似乎使用了什么),并将所有函数改为它们的wchar_t对象也不会做。
In Windows, that doesn't work. Converting the file into UCS-2 Little Endian (what Windows seems to use by default) and changing all the functions into their wchar_t counterparts doesn't do the trick either.
推荐答案
是否有某种正确的方式来完成这些工作? Windows控制台支持unicode,排序。它不支持从左到右和复杂脚本。要使用Visual C ++打印UTF-16文件,请使用以下命令:
The Windows console supports unicode, sort of. It does not support left-to-right and "complex scripts". To print a UTF-16 file with Visual C++, use the following:
_setmode(_fileno(stdout), _O_U16TEXT);
并使用 wcout
$ c> cout 。
And use wcout
instead of cout
.
不支持UTF8代码页,因此对于UTF-8,您必须使用 MultiBytetoWideChar
There is no support for a "UTF8" code page so for UTF-8 you will have to use MultiBytetoWideChar
有关unicode的控制台支持的更多信息可以在
More on console support for unicode can be found in this blog
这篇关于C ++:在Windows中将Unicode文件的内容输出到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!