473,378 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

May 23 '06 #1
7 30937

"abbu" <vi********@rediffmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...

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
May 23 '06 #2
abbu <vi********@rediffmail.com> wrote:
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';
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 23 '06 #3
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
May 23 '06 #4
* Marcus Kwok:
abbu <vi********@rediffmail.com> wrote:
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.

--
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?
May 23 '06 #5
Alf P. Steinbach <al***@start.no> wrote:
* 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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 23 '06 #6
abbu wrote:
int main()
{
wchar_t p[]="Good Morning";
}


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

May 23 '06 #7
Thanks eric.

May 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Julius Mong | last post by:
Hi all, I'm doing this: // Test char code wchar_t lookup = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01}; for (int x=0; x<5; x++) { wchar_t * string = (wchar_t*) malloc(sizeof(wchar_t)); string =...
6
by: Sumpfman | last post by:
Hi there, i really got stuck on working with this. The user should be able to set his usr_delay as argument and later on i want to do some math with this (adding usr_delay to the time i get from...
11
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but...
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
7
by: sonu | last post by:
Hi all, from my code i want the out put should be 502 but its giving 7 pls any one try to correct. (Actualy i want to write a similar function as atol() which takes const char * and gives long...
0
by: Jose Cintron | last post by:
I have a program that reads an XML file and extract a specific filed from the file. The filed is "free" form text, here's an example === start example text Ensure that individual users are not...
3
by: Angus | last post by:
I can see how to get a char* but is it possible to get a wide char - eg wchar_t?
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
5
by: yakir22 | last post by:
Hello experts, I am dealing now in porting our server from windows to linux. our client is running only on windows machine. to avoid the wchar_t size problem ( in windows its 2 bytes and linux is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.