?
Solved

How to deal with std::wstring with Visual Studio

Posted on 2013-12-01
6
Medium Priority
?
742 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 35

Accepted Solution

by:
sarabande earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…
Suggested Courses

864 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