std::wcout 到 Xcode 中的控制台窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/276010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
std::wcout to console window in Xcode
提问by philsquared
In an Xcode project, if I use std::cout
to write to the console the output is fine.
在 Xcode 项目中,如果我std::cout
用来写入控制台,则输出很好。
However, if I use std::wcout
I get no output.
但是,如果我使用,则std::wcout
没有输出。
I know that this is a thorny issue in C++, and I've been googling around to try and find a specific solution in the Xcode case. A couple of things I found that it was suggested should work were:
我知道这是 C++ 中的一个棘手问题,我一直在谷歌搜索以尝试在 Xcode 案例中找到特定的解决方案。我发现建议应该起作用的几件事是:
std::cout.imbue( std::locale("") );
and
和
std::setlocale(LC_ALL, "");
Neither of these have made any difference. Before I resign myself to spending the next couple of weeks studying the facets API just to be able to write to the console I thought I'd check with the esteemed audience here.
这些都没有产生任何区别。在我辞职花接下来的几周研究 facets API 只是为了能够写入控制台之前,我想我会在这里与尊敬的观众进行核实。
[Update]
[更新]
I think the reason for the problem I've been having is actually to do with the specific encoding of some of the strings I'm trying to print.
我认为我遇到的问题的原因实际上与我尝试打印的某些字符串的特定编码有关。
If I send just a string literal, using the L"my string" syntax
it works! It appears this is using UTF32 - little endian encoding.
如果我只发送一个字符串文字,使用L"my string" syntax
它就可以了!看来这是使用 UTF32 - 小端编码。
However, I've been mixing this with strings I've been passed from Objective C++ code using NSUTF32BigEndianStringEncoding encoding. It's this mix of encodings that's causing the problems.
但是,我一直在将它与使用 NSUTF32BigEndianStringEncoding 编码从 Objective C++ 代码传递的字符串混合在一起。正是这种编码的混合导致了这些问题。
I think we can consider this matter closed. Thanks for reading.
我想我们可以考虑关闭这件事。谢谢阅读。
回答by Martin York
std::wcout should work just like std::cout.
std::wcout 应该像 std::cout 一样工作。
The following works fine on my MAC:
以下在我的 MAC 上运行良好:
#include <iostream>
int main()
{
std::cout << "HI" << std::endl;
std::wcout << L"PLOP" << std::endl;
}
Maybe (though some code would have been nice) its because you are not flushing the buffer. Remember that std::cout and std::wcout are buffered. This means the output will not be pushed to the console until the buffer is filled or you explicitly flush the buffer.
也许(尽管有些代码会很好)是因为您没有刷新缓冲区。请记住 std::cout 和 std::wcout 是缓冲的。这意味着在填充缓冲区或您明确刷新缓冲区之前,不会将输出推送到控制台。
You can flush the buffer with:
您可以使用以下方法刷新缓冲区:
std::wcout << flush();
// or
std::wcout << endl; // Those also puts a '\n' on the stream.