Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 23rd, 2006, 03:55 PM
abbu
Guest
 
Posts: n/a
Default how to output the wchar_t type string


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

  #2  
Old May 23rd, 2006, 04:05 PM
Eric Jensen
Guest
 
Posts: n/a
Default Re: how to output the wchar_t type string


"abbu" <vignesh_cn@rediffmail.com> wrote in message
news:1148395831.478182.220500@i39g2000cwa.googlegr oups.com...[color=blue]
>
> 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?[/color]

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


  #3  
Old May 23rd, 2006, 04:05 PM
Marcus Kwok
Guest
 
Posts: n/a
Default Re: how to output the wchar_t type string

abbu <vignesh_cn@rediffmail.com> wrote:[color=blue]
> 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?[/color]

The following program works fine here:


#include <iostream>

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



--
Marcus Kwok
Replace 'invalid' with 'net' to reply
  #4  
Old May 23rd, 2006, 04:25 PM
Eric Jensen
Guest
 
Posts: n/a
Default Re: how to output the wchar_t type string

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


  #5  
Old May 23rd, 2006, 04:35 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: how to output the wchar_t type string

* Marcus Kwok:[color=blue]
> abbu <vignesh_cn@rediffmail.com> wrote:[color=green]
>> 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?[/color]
>
> The following program works fine here:
>
>
> #include <iostream>
>
> int main()
> {
> wchar_t p[] = L"Good Morning";
> std::wcout << p << '\n';
> }[/color]

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #6  
Old May 23rd, 2006, 05:55 PM
Marcus Kwok
Guest
 
Posts: n/a
Default Re: how to output the wchar_t type string

Alf P. Steinbach <alfps@start.no> wrote:[color=blue]
> * Marcus Kwok:[color=green]
>> The following program works fine here:
>>
>>
>> #include <iostream>
>>
>> int main()
>> {
>> wchar_t p[] = L"Good Morning";
>> std::wcout << p << '\n';
>> }[/color]
>
> 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.[/color]

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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
  #7  
Old May 23rd, 2006, 10:25 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: how to output the wchar_t type string

abbu wrote:[color=blue]
> int main()
> {
> wchar_t p[]="Good Morning";
> }[/color]

You need an L ahead of the text literal to make it wide.

  #8  
Old May 24th, 2006, 09:45 AM
abbu
Guest
 
Posts: n/a
Default Re: how to output the wchar_t type string

Thanks eric.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.