[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to deal with std::wstring with Visual Studio

Posted on 2013-12-01
6
Medium Priority
?
737 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

650 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