相关疑难解决方法(0)

将Unicode UTF-8文件读入wstring

如何wstring在Windows平台上读取(UTF-8)文件?

c++ unicode file utf-8 wstring

37
推荐指数
5
解决办法
6万
查看次数

如何在Windows上将UTF-8字符串打印到std :: cout?

我正在用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)))) …

c++ windows encoding utf-8

18
推荐指数
3
解决办法
2万
查看次数

在Windows控制台中正确打印utf8字符

这是我尝试这样做的方式:

#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)
  • 这也行不通.效果是一样的.我的字体当然是Lucida Console.

第三步:

#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)

c++ console mingw utf-8 windows-xp-sp3

16
推荐指数
3
解决办法
2万
查看次数

C++ Visual Studio字符编码问题

无法绕过这一个是一个真正的耻辱源...

我在法语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++ character-encoding visual-studio

15
推荐指数
3
解决办法
2万
查看次数

在C++中设置编码的最正确方法是什么?

如何最好在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>和许多其他人.

最后我被这种萨满教所困扰,我想问你:如何建立编码是正确的?

c++ windows unicode encoding utf

8
推荐指数
1
解决办法
9206
查看次数

std::wcout 打印 unicode 字符但它们被隐藏

所以,下面的代码:

\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}\n
Run 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

c++ unicode

3
推荐指数
1
解决办法
895
查看次数

字符串和特殊字符的奇怪出现

#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不同?

c++ string character-encoding special-characters

0
推荐指数
1
解决办法
246
查看次数

Unicode 字符 Visual C++

我试图让我的程序使用 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 上,它是不同的。

谢谢你。

c++ unicode utf-8 character-encoding visual-studio-2010

0
推荐指数
1
解决办法
3009
查看次数