assigning a TCHAR char array to std::string

Reply

Join Date: Aug 2007
Posts: 18
Reputation: Ashu@sym is an unknown quantity at this point 
Solved Threads: 0
Ashu@sym Ashu@sym is offline Offline
Newbie Poster

assigning a TCHAR char array to std::string

 
0
  #1
Sep 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Sep 20th, 2007
What error do you get?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,142
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

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

 
0
  #3
Sep 20th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 18
Reputation: Ashu@sym is an unknown quantity at this point 
Solved Threads: 0
Ashu@sym Ashu@sym is offline Offline
Newbie Poster

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

 
0
  #4
Sep 20th, 2007
Originally Posted by Narue View Post
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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 18
Reputation: Ashu@sym is an unknown quantity at this point 
Solved Threads: 0
Ashu@sym Ashu@sym is offline Offline
Newbie Poster

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

 
0
  #5
Sep 20th, 2007
Originally Posted by Ancient Dragon View Post
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??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #6
Sep 20th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 18
Reputation: Ashu@sym is an unknown quantity at this point 
Solved Threads: 0
Ashu@sym Ashu@sym is offline Offline
Newbie Poster

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

 
0
  #7
Sep 21st, 2007
Originally Posted by Narue View Post
>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!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC