how to output the wchar_t type string

A

abbu

int main()
{
wchar_t p[]="Good Morning";
}

How to use cout on p.
That is, can I use cout<<p;

It's not working.

Can anybody suggest how to use cout on wchar_t type strings?

Thanks
Vignesh
 
E

Eric Jensen

abbu said:
int main()
{
wchar_t p[]="Good Morning";
}

How to use cout on p.
That is, can I use cout<<p;

It's not working.

Can anybody suggest how to use cout on wchar_t type strings?

int main() {
wchar_t p[]="Good Morning";
std::wcout << p;
}

Mostly when it comes to wide strings you just use the normal funcs but add
"w" to the start.
eg.:

const char *str = "text";
printf(str);

const wchar_t *str = "text";
wprintf(str);

cout outputs a standard byte stream (ansi)
wcout outputs a wide stream (e.g. unicode)

//eric
 
M

Marcus Kwok

abbu said:
int main()
{
wchar_t p[]="Good Morning";
}

How to use cout on p.
That is, can I use cout<<p;

It's not working.

Can anybody suggest how to use cout on wchar_t type strings?

The following program works fine here:


#include <iostream>

int main()
{
wchar_t p[] = L"Good Morning";
std::wcout << p << '\n';
}
 
E

Eric Jensen

After posting this i noticed that i made bad usage of printf and wprintf.

In case some people new to programming sees this, i better point out that
this is bad:

char *str = "text";
printf(str);

If str has "%s" inside it will crash the app.

Correct usage:

char *str = "text";
printf("%s", str);

//eric
 
A

Alf P. Steinbach

* Marcus Kwok:
abbu said:
int main()
{
wchar_t p[]="Good Morning";
}

How to use cout on p.
That is, can I use cout<<p;

It's not working.

Can anybody suggest how to use cout on wchar_t type strings?

The following program works fine here:


#include <iostream>

int main()
{
wchar_t p[] = L"Good Morning";
std::wcout << p << '\n';
}

g++ 3.4.4 on Windows does not support wide character streams.

Also be aware that what the wide characters streams do, is to convert to
narrow characters, in some /unspecified/ way.
 
M

Marcus Kwok

Alf P. Steinbach said:
* Marcus Kwok:
The following program works fine here:


#include <iostream>

int main()
{
wchar_t p[] = L"Good Morning";
std::wcout << p << '\n';
}

g++ 3.4.4 on Windows does not support wide character streams.

Also be aware that what the wide characters streams do, is to convert to
narrow characters, in some /unspecified/ way.

I guess I should have mentioned that this was using VC++ 7.1 (VS .NET
2003) on Windows XP.

Thanks for the info regarding wide streams. I never use them but this
example seemed easy enough to whip together.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top