write wstring to standard output?

Pages: 12

How do you write a wstring to std output?


wstring str( L"testing" );
std::cout << str << std::endl;


I assume this doesn't work because cout is an ostream object, and i need to write to a wostream object. So, whats the wostream object? i tried wcout, and it didn't work, not surprisingly

thanks
wcout works for me.
er, i tried wcout and std::wcout and it keeps telling me

`wcout' undeclared (first use this function)

is it in a different namespace?
my IDE is most recent Dev-C++, if that matters

thanks
It won't work the way you think it ought.

wcout doesn't properly write wide characters to the output device -- it calls narrow() on each wchar_t to convert it to a char, then outputs it just like cout does...
I'd say the sanest thing to do is convert the string to UTF-8 (or some other multi-byte encoding) and write that to std::cout. The standard streams aren't very good for wide characters.

Dev-C++'s MinGW is outdated and cripplingly non-conforming.
yes, use MS visual studio express or code::blocks..
You can just download and install the latest version of MinGW and use it with Dev-C++.
Duoas wrote:
You can just download and install the latest version of MinGW and use it with Dev-C++.
yes indeed. . .
well, the whole point was to output spanish letters, which dont have ASCII codes so I need UTF-16 at least. and the ultimate goal is to write them to a window with drawtext() or something similar. Is there a function similar to drawtext that handles wide characters?

yeah, i heard Dev C++ was pretty bad, but it was free, so yeah.
can i just use the latest MinGW with Dev C++? I'll have to look into that

thanks
Do you need to do this portably? If you do, this is a lost battle. If you don't, there are ways to do it. For example, you could assume a given code page is being used (I can't remember at the moment what the Windows equivalent of ISO 8859-1 is) and output single byte characters.
maybe you can talk with japanese programmers, i always wonder how they print japanese letters in console...
I don't. I know exactly how they do it. >:-(
really? there's no standard unicode tables?
wait, DrawText() takes a LPCTSTR, which, as i understand it is a pointer to wchar_t. So i guess I can just create a wstring and output it.

I'm just trying to have letters with accents, so i doubt it needs anything fancy
DrawText() also only works on GDCs. Not the console.

The default console Code Page supports all the special Spanish letters. Compile and run this to see everything in there.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iomanip>
#include <iostream>
using namespace std;

int main()
  {
  int c, n;

  cout << setw( 5 ) << "+";
  for (n = 0; n < 16; n++)
    cout << setw( 3 ) << n;
  cout << endl;

  for (n = 32, c = 32; c < 254; c++)
    {
    if ((n % 16) == 0)
      cout << setw( 5 ) << n;

    cout << setw( 3 ) << (char)c;

    if ((++n % 16) == 0)
      cout << endl;
    }
  cout << endl;

  return 0;
  }

The characters 129 and 160-168 and 173-175 are all used in Spanish. You just need to convert your Unicode strings to the default MS console codepage (8-bit characters) before outputting your strings.

You should be able to do this with a simple function that returns a modified string, which you can then pass to [w]cout.

Hope this helps.
Yeah, my goal isn't to write to the console, I'm trying to use DrawText() to draw to a window. I have the window part working fine, i just wanted to draw Spanish characters. If the Spanish characters have ASCII codes, can I just avoid wstrings altogether?
The Spanish characters do not have ASCII codes. They are in the default code page Microsoft uses with the console.

However, since you aren't interested in the console and are instead using DrawText(), you shouldn't have any problem. What is it that isn't working for you?

(Did you make sure to select a Unicode font to draw with?)


[edit] BTW, I just found this beautiful site:
http://www.jorgon.freeserve.co.uk/GoasmHelp/Unicode.htm
Last edited on
They have extended ASCII codes
http://www.cplusplus.com/doc/ascii/

so, I don't think I have a problem anymore, unless the extended ASCII isn't portable

thanks for the link, its really useful
It isn't. Technically, not even ASCII is portable.
so you're saying that ASCII might not work on other people's computers?

is extended ASCII any less portable than ASCII, or is there a more portable way to output spanish characters?
Pages: 12