943,667 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 27616
  • C++ RSS
Sep 20th, 2007
0

assigning a TCHAR char array to std::string

Expand Post »
I want to assign a TCHAR char array to std::string in VC++ 2005, how should i do that? a simple assignment gives error i.e

TCHAR buffer[MAX-1];
std::string data;
//some operation for filling the buffer

data = buffer; //This gives error

Plz help me in doing this operation.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ashu@sym is offline Offline
18 posts
since Aug 2007
Sep 20th, 2007
0

Re: assigning a TCHAR char array to std::string

What error do you get?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 20th, 2007
0

Re: assigning a TCHAR char array to std::string

UNICODE is the default setting for that compiler so if you didn't turn it off then the compiler is mapping TCHAR into wchar_t. go to Project --> Properties (bottom menu item) --> Configuration Properties --> General then change Character Set option to Not Set. If you really do want UNICODE then you have to use one of the conversion functions to convert wchar_t* to char*
Last edited by Ancient Dragon; Sep 20th, 2007 at 9:41 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Sep 20th, 2007
0

Re: assigning a TCHAR char array to std::string

Click to Expand / Collapse  Quote originally posted by Narue ...
What error do you get?
I tried to build this code in Visual C++ 2005 and i got this error:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'TCHAR [260]' (or there is no acceptable conversion)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ashu@sym is offline Offline
18 posts
since Aug 2007
Sep 20th, 2007
0

Re: assigning a TCHAR char array to std::string

UNICODE is the default setting for that compiler so if you didn't turn it off then the compiler is mapping TCHAR into wchar_t. go to Project --> Properties (bottom menu item) --> Configuration Properties --> General then change Character Set option to Not Set. If you really do want UNICODE then you have to use one of the conversion functions to convert wchar_t* to char*

i want to do in UNICODE only so can u suggest me those conversion functions??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ashu@sym is offline Offline
18 posts
since Aug 2007
Sep 20th, 2007
0

Re: assigning a TCHAR char array to std::string

>error C2679: binary '=' : no operator found which takes a
>right-hand operand of type 'TCHAR [260]'
Yep, it looks like you're trying to initialize a narrow string with an array of wide characters. Try using std::wstring instead of std::string.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 21st, 2007
0

Re: assigning a TCHAR char array to std::string

Click to Expand / Collapse  Quote originally posted by Narue ...
>error C2679: binary '=' : no operator found which takes a
>right-hand operand of type 'TCHAR [260]'
Yep, it looks like you're trying to initialize a narrow string with an array of wide characters. Try using std::wstring instead of std::string.

Thanks Narue!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ashu@sym is offline Offline
18 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with list view
Next Thread in C++ Forum Timeline: Terminal size





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC