String to std::wcout?


#1

Pardon the stupidity…but how do I output a string to std::cout in a Juce console app??

EDITED to change std::cout to std::wcout


#2

std::cout << str.toCString()


#3

Actually, the latest juce versions don’t have toCString() anymore.

I think something like std::cout<<str.toUTF8().getAddress(); should work though


#4

what’s the secret to getting it standard out, but as wchar_t ??


#5

That’s compiler specific, but I think under VS, with the default options (/Zc:wchar_t), wchar_t is defined as multibyte, 16 bit char, so you might want to try using toUTF16 ?!
I’m not that good with unicode concepts but you might have a look here for more information …


#6

[quote=“dinaiz”]That’s compiler specific, but I think under VS, with the default options (/Zc:wchar_t), wchar_t is defined as multibyte, 16 bit char, so you might want to try using toUTF16 ?!
I’m not that good with unicode concepts but you might have a look here for more information …[/quote]

Haha well played. I posted that link last year when there was talk of revising the juce String class.

Apparently, juce::String has a global operator to a std::basic_ostream which is pretty cool, so this worked:

  String s;
  //...
  std::cout << s << std::endl;

But this goes through toUTF8. What I was looking for is a cross platform solution that sends the juce String in a wide string, something like this:

  String s;
  //...
  std::wcout << s << std::endl;

Note the use of std::wcout but I think this is sending it to wcout as a utf-8 string which is wrong.


#7

You could certainly do


#8

Yes that was half of the puzzle! I finally got it all sorted out. I added this function to juce_String.h

template <class traits> std::basic_ostream <wchar_t, traits>& JUCE_CALLTYPE operator<< (std::basic_ostream <wchar_t, traits>& stream, const String& stringToWrite) { return stream << stringToWrite.toWideCharPointer(); }

It gets called for “std::wcout <<s;” where s is type juce::String. It does not affect operator << for std::cout (I tested it).

So if you want to add it…


#9

Ah, good suggestion, thanks!