-
August 6th, 2010, 12:22 PM
#1
Concat LPCTSTR
Hi
I have a string LPCTSTR. Now I want to append that to a string or concat it.
For e.g.
LPCTSTR str_name;
// str_name gets a string value from a function....
//.. say right now str_name ="ABC123"
Now I want a new string which would be like "Employee_ABC123"
So, all I want to do is either append 'ABC123' to 'Employee_'
or concat 'Employee_' and 'ABC123'
I tried LPCTSTR name = lstrcat(_T("Employee_"),str_name);
and also lstrcatw, but I am not getting data in 'name' !
I want the result in LPCTSTR .. if possible...
Actually the result is supposed to be any of the COLeVariant data types..!
Any Suggestions??
-
August 6th, 2010, 02:50 PM
#2
Re: Concat LPCTSTR
Originally Posted by guptesanket
Hi
I have a string LPCTSTR
That is not a string. It is a constant pointer.
Now I want to append that to a string or concat it.
You can't. It is just a pointer, not a string, and furthermore, it's const, so whatever it's pointing to cannot be changed.
Are you coding in C++? If you are, why not use CString, std::string, std::wstring, or some other string classes, instead of trying to do things with pointers?
Code:
LPCTSTR str_name = _T("Hello ");
CString s = str_Name + "_ABC123";
//...
I tried LPCTSTR name = lstrcat(_T("Employee_"),str_name);
and also lstrcatw, but I am not getting data in 'name' !
You're lucky the code did not crash. You cannot copy or change a string literal.
First, you must have a modifiable buffer. That means it must be a true array of characters (not a literal).
Second, the buffer must be big enough to hold all of the characters you're adding.
You can do this, or do things the easier and safer way by using string classes (such as CString, std::string, etc.)
I want the result in LPCTSTR .. if possible...
A pointer is not a string. The pointer to one of the entries in the string is an LPCTSTR.
Regards,
Paul McKenzie
-
August 6th, 2010, 05:13 PM
#3
Re: Concat LPCTSTR
All right... !
I knew that in LPCSTR/LPCTSTR P meant pointer and C meant const... ! But it never occurred to me that its a constant pointer and cannot be concatenated or appended... !
Thank you... ! You enlightened me ! I think I would not forget this in future.. ! i am a newbie ! and yes... it is VC++ !
I will try it out with std::string !
Thanks again !
-
August 9th, 2010, 11:27 AM
#4
Re: Concat LPCTSTR
Originally Posted by guptesanket
All right... !
I knew that in LPCSTR/LPCTSTR P meant pointer and C meant const... ! But it never occurred to me that its a constant pointer and cannot be concatenated or appended... !
Thank you... ! You enlightened me ! I think I would not forget this in future.. ! i am a newbie ! and yes... it is VC++ !
I will try it out with std::string !
Thanks again !
If you are using Visual C++ and a fairly recent compiler (newer than 10 years old), then consider using CString over std::string or std:wstring.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|