如何wstring在Windows平台上读取(UTF-8)文件?
我正在用C++编写一个跨平台的应用程序.所有字符串都在内部进行UTF-8编码.请考虑以下简化代码:
#include <string>
#include <iostream>
int main() {
std::string test = u8"Greek: ????; German: Übergrößenträger";
std::cout << test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Unix系统上,std::cout期望8位字符串是UTF-8编码的,所以这段代码工作正常.
但是,在Windows上,要求std::cout8位字符串采用Latin-1或类似的非Unicode格式(取决于代码页).这导致以下输出:
希腊语:????????; 德语:?£bergr?Âentr?ñger
如何std::cout在Windows上将8位字符串解释为UTF-8?
这是我试过的:
#include <string>
#include <iostream>
#include <io.h>
#include <fcntl.h>
int main() {
_setmode(_fileno(stdout), _O_U8TEXT);
std::string test = u8"Greek: ????; German: Übergrößenträger";
std::cout << test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望_setmode能做到这一点.但是,这会在调用的行中导致以下断言错误operator<<:
Microsoft Visual C++运行时库
调试断言失败!
程序:d:\ visual studio 2015\Projects\utf8test\Debug\utf8test.exe文件:minkernel\crts\ucrt\src\appcrt\stdio\fputc.cpp行:47
表达式:((_Stream.is_string_backed())||(fn = _fileno(_Stream.public_stream()),((_ textmode_safe(fn)== __crt_lowio_text_mode :: ansi)&&!_ tm_unicode_safe(fn)))) …
这是我尝试这样做的方式:
#include <stdio.h>
#include <windows.h>
using namespace std;
int main() {
SetConsoleOutputCP(CP_UTF8);
//german chars won't appear
char const* text = "aäbcdefghijklmnoöpqrsßtuüvwxyz";
int len = MultiByteToWideChar(CP_UTF8, 0, text, -1, 0, 0);
wchar_t *unicode_text = new wchar_t[len];
MultiByteToWideChar(CP_UTF8, 0, text, -1, unicode_text, len);
wprintf(L"%s", unicode_text);
}
Run Code Online (Sandbox Code Playgroud)
效果是只显示我们的ascii字符.没有显示错误.源文件以utf8编码.
那么,我在这里做错了什么?
到WouterH:
int main() {
SetConsoleOutputCP(CP_UTF8);
const wchar_t *unicode_text = L"aäbcdefghijklmnoöpqrsßtuüvwxyz";
wprintf(L"%s", unicode_text);
}
Run Code Online (Sandbox Code Playgroud)
第三步:
#include <stdio.h>
#define _WIN32_WINNT 0x05010300
#include <windows.h>
#define _O_U16TEXT 0x20000
#include <fcntl.h>
using namespace std;
int main() { …Run Code Online (Sandbox Code Playgroud) 无法绕过这一个是一个真正的耻辱源...
我在法语Windows(XP)中使用法语版的Visual Studio(2008).发送到输出窗口的字符串中的法语重音被破坏.从输出窗口输入Ditto .典型的字符编码问题,我输入ANSI,得到UTF-8作为回报,或者那样的东西.在向输出窗口显示"硬编码"字符串时,什么设置可以确保字符保留在ANSI中?
编辑:
例:
#include <iostream>
int main()
{
std:: cout << "àéêù" << std:: endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将在输出中显示:
奥羽
(此处编码为HTML以供您观看乐趣)
我真的很想表明:
àéêù
如何最好在C++中设置编码?
我习惯了使用Unicode(和工作wchar_t,wstring,wcin,wcout和L "...").我还以UTF-8保存了源代码.
目前我使用MinGW(Windows 7)并在Windows控制台(cmd.exe)中运行我的程序,但有时我可以在GNU\Linux上使用gcc并在Linux控制台中使用UTF-8编码运行promgram.
在任何时候我都希望在Windows和Linux上编译我的源代码,我希望所有的Unicode符号都被正确地输入和输出.
当我遇到编码的下一个问题时,我用Google搜索.此外,我发现的最不同的委员会:setlocale(LC_ALL, "")和setlocale(LC_ALL, "xx_XX.UTF-8"),std::setlocale(LC_ALL, "")并std::setlocale(LC_ALL, "xx_XX.UTF-8")从<clocale>,
SetConsoleCP()而SetConsoleOutputCP()从<windows.h>和许多其他人.
最后我被这种萨满教所困扰,我想问你:如何建立编码是正确的?
所以,下面的代码:
\n#include <iostream>\n#include <string>\n#include <io.h>\n#include <fcntl.h>\n#include <codecvt>\n\nint main()\n{\n setlocale(LC_ALL, "");\n\n std::wstring a;\n std::wcout << L"Type a string: " << std::endl;\n std::getline(std::wcin, a);\n std::wcout << a << std::endl;\n getchar();\n}\nRun Code Online (Sandbox Code Playgroud)\n当我输入“\xc3\xa5\xc3\xa4\xc3\xb6”时,我得到一些奇怪的输出。终端的光标是缩进的,但后面没有文本。如果我使用右箭头键向前移动光标,则当我单击右箭头键时,“\xc3\xa5\xc3\xa4\xc3\xb6”会显示出来。
\n如果我包含英文字母,以便输入为“hello\xc3\xa5\xc3\xa4\xc3\xb6”,则输出为“hello”,但当我单击右箭头键“hello\xc3\xa5\xc3\xa4\”时xc3\xb6" 一个字母一个字母地出现。
\n为什么会发生这种情况,更重要的是我该如何解决它?
\n编辑:我在 Windows 上使用 Visual Studio 的编译器进行编译。当我在 repl.it 中尝试这个确切的代码(他们使用 clang)时,它就像一个魅力。问题是由我的代码、Windows 还是 Visual Studio 引起的吗?
\n#include <iostream>
#include <string>
using namespace std;
string mystring1, mystring2, mystring3 = "grové";
int main(){
mystring1 = "grové";
getline( cin, mystring2 ); //Here I type "grové" (without "")
cout << "mystring1= " << mystring1 << endl;
cout << "mystring2= " << mystring2 << endl;
cout << "mystring3= " << mystring3 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是:
mystring1 = grov8
mystring2 =grovémystring3
= grov8
虽然当我在这里剪切并粘贴代码时它来自:
mystring1 =grovΘmystring2
=grovémystring3
=grovΘ
为什么mystring2的内容与mystring1和mystring3不同?
我试图让我的程序使用 unicode 字符。我在 Windows 7 x32 机器上使用 Visual Studio 2010。
我想打印的是皇后符号(“\ ul2655”),但它不起作用。我已将我的解决方案设置为使用 unicode。
这是我的示例代码:
#include <iostream>
using namespace std;
int main()
{
SetConsoleOutputCP(CP_UTF8);
wcout << L"\u2655";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此外,我尝试了许多其他建议,但没有任何效果。(例如,更改cmd字体,应用chcp 65001,与SetConsoleOutputCP(CP_UTF8)等相同)。
问题是什么?我还是第一次遇到这样的情况。在 linux 上,它是不同的。
谢谢你。