?
Solved

How to deal with std::wstring with Visual Studio

Posted on 2013-12-01
6
Medium Priority
?
728 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

762 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