CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Concat LPCTSTR

  1. #1
    Join Date
    Jul 2010
    Location
    Baltimore, MD, USA
    Posts
    84

    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??

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Concat LPCTSTR

    Quote Originally Posted by guptesanket View Post
    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

  3. #3
    Join Date
    Jul 2010
    Location
    Baltimore, MD, USA
    Posts
    84

    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 !

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Concat LPCTSTR

    Quote Originally Posted by guptesanket View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured