Solved

How to deal with std::wstring with Visual Studio

Posted on 2013-12-01
6
686 Views
Last Modified: 2013-12-07
i am designing a program to read records of data into a vector of structure

typedef struct _recordEntry
{
  wstring  name;
  wstring  description;
  int         ID;
}  RECORDENTRY;

In the file,  I have records like the following
name1, "This is name1 description", 123
name2, "This is name2 description", 234
name3, "This is name3 description", 345

I am trying to read in a line at a time, then tokenize using boost::tokenizer.

My question is how do I create a visual studio project that will use wstring and how can I use corresponding boost::tokenizer that is for wstring?
0
Comment
Question by:tommym121
  • 3
  • 2
6 Comments
 

Author Comment

by:tommym121
Comment Utility
I was able to read in but when I try to

RECORDENTRY *pRecordEntry;


std::cout  << "Record:  "<< pRecordEntry->name << ";" << pRecordEntry->description << std::endl;

I got this out put

Record: 00C50A0C;00C53790
0
 

Author Comment

by:tommym121
Comment Utility
But when I  'Add Watch" to view the contenct , the right info is inside the structure.

How do I display wstring content?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
Comment Utility
To output wstrings, you need to use 'wcout', instead of 'cout', i.e.

std::wcout  << "Record:  "<< pRecordEntry->name << ";" << pRecordEntry->description << std::endl;

Open in new window

That's pretty much it.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
BTW, you get 'Record: 00C50A0C;00C53790' because 'cout' interprets UNICODE strings as a hexadecimal value. That's why you need 'wcout' instead, as it interprets them correctly as UNICODE text.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 250 total points
Comment Utility
My question is how do I create a visual studio project that will use wstring and how can I use corresponding boost::tokenizer that is for wstring?

contrary to mfc CString and TCHAR which are either MBCS (ANSI) or UNICODE (UTF16), the std::string and std::wstring can be used parallel in any c++ project. you don't have to decide between the both. that is the good news. the bad one is that  in visual studio and vc compiler it is not very convenient to transform the one to the other.

your cvs file has ansi texts. because of that the tokenizer needs to operate on char type and a wide string tokenizer would not work. in the record structure you want to have wide strings. you firstly should check whether that really is necessary and would not generate more issues than it helps to avoid. a wide string converted from an ansi string does not bear more information as the original text. as you have seen, for the std::cout the support of mixed strings in vc compiler and its implementation of the standard library are poor to nothing. so, one way to go forward is to do the transformation as late as possible, what means only then when you actually need a wide string in a function call. the second way would be to make the transformation as early as possible. in your case it would be when the std::string was returned from tokenizer and you would assign a wstring member of the record. in case your input is ascii only, a simple transformation from string to wstring easily can be made by the following function:

std::wstring strToWstr(const std::string & s)
{
     size_t len = s.length() ;
     std::wstring ws(len, L' ');
     for (size_t n = 0; n < len; ++n)
           ws[n] = (wchar_t)s[n];
     return ws;
}
....
Record rec;
...
     rec.name = strToWstr("John");
     rec.description = strToWstr(strAsciiDesc);

Open in new window


Sara
0
 

Author Closing Comment

by:tommym121
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now