Solved

How to deal with std::wstring with Visual Studio

Posted on 2013-12-01
6
688 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
ID: 39688806
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
ID: 39688810
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
ID: 39688850
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 39688854
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 33

Accepted Solution

by:
sarabande earned 250 total points
ID: 39689640
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
ID: 39703279
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

947 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

19 Experts available now in Live!

Get 1:1 Help Now