views:

1153

answers:

1

In an XCode project, if I use std::cout to write to the console the output is fine.

However, if I use std::wcout I get no output.

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:


    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 audidence here.

[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.

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.

I think we can consider this matter closed. Thanks for reading.

+3  A: 

std::wcout should work just like std::cout.

The following works fine on my 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.

You can flush the buffer with:

std::wcout << flush();
// or
std::wcout << endl;  // Those also puts a '\n' on the stream.
Martin York
Thanks for the reply. I think I have found the reason for my problems - updating my question now...
Phil Nash