【问题标题】:Why doesn't this simple program work? [duplicate]为什么这个简单的程序不起作用? [复制]
【发布时间】:2013-12-04 14:31:49
【问题描述】:

我正在尝试整理有关 Unicode 在 Windows 控制台应用程序中的工作方式的所有内容。为什么这个简单的程序不起作用?

#include <iostream>

int wmain(int argc, const wchar_t* const argv[])
{
    for (int i = 1; i < argc; ++i)
        std::wcout << argv[i] << std::endl;

    return 0;
}

编译

>cl /EHsc /D _UNICODE /D UNICODE /Zc:wchar_t test.cpp

程序产生

> test.exe 1 2 3 abc абв
1
2
3
abc

第五个参数在哪里?我必须提到абв 适合我的GetACP()GetConsoleCP()GetConsoleOutputCP() 代码页(1251、866 和866)。有趣的是,该程序(在某种程度上)与这些代码页之外的字符一起工作:

> test.exe Sæter
Sцter

但是:

> test.exe абв Sæter

【问题讨论】:

  • 如果你做test.exe 1 2 3 abc абв &gt; output.txt,输出文件是否正确?
  • @Benoit 不是,还是没有最后一行。
  • 试试这个:printf("%d\n", argc);。你得到了什么?

标签: c++ visual-c++ unicode


【解决方案1】:

我认为这是因为wcout 转换为窄字符。

请参阅this other question 了解此主题。

Another good link.

【讨论】:

  • 那么转换成窄字符有什么特别之处呢? абв 属于我使用的代码页。
  • 好的,std::wcout 确实设置了badbit。想知道为什么会发生这种情况,需要进行适当的调查。
猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 2018-07-17
  • 2011-10-25
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多