DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   assigning a TCHAR char array to std::string (http://www.daniweb.com/forums/thread90163.html)

Ashu@sym Sep 20th, 2007 4:14 am
assigning a TCHAR char array to std::string
 
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.

Narue Sep 20th, 2007 9:22 am
Re: assigning a TCHAR char array to std::string
 
What error do you get?

Ancient Dragon Sep 20th, 2007 9:39 am
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*

Ashu@sym Sep 20th, 2007 10:41 am
Re: assigning a TCHAR char array to std::string
 
Quote:

Originally Posted by Narue (Post 437817)
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)

Ashu@sym Sep 20th, 2007 10:42 am
Re: assigning a TCHAR char array to std::string
 
Quote:

Originally Posted by Ancient Dragon (Post 437823)
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??

Narue Sep 20th, 2007 10:52 am
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.

Ashu@sym Sep 21st, 2007 12:16 am
Re: assigning a TCHAR char array to std::string
 
Quote:

Originally Posted by Narue (Post 437884)
>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!!


All times are GMT -4. The time now is 9:43 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC