Results 1 to 4 of 4

Thread: Concat Strings

  1. #1

    Thread Starter
    Hyperactive Member jovton's Avatar
    Join Date
    Nov 2000
    Location
    South Africa
    Posts
    266

    Question

    How do I go about concatenating two strings?
    I know I should use lstrcat for it, but how?
    I get a lot of Access Violation errors,
    and the lstrcat just don't want to work.

    For example...

    I want to concatenate the strings "First " and "Second ", to form "First Second ".
    Then I want to concatenate the result with "Third", to form "First Second Third".

    I know it must not be that hard, but for me it is.
    Help, anyone?
    Thanx.
    jovton

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try just using strcat. Or alternatively, my preferred method is to do it myself, or use the string class:
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::string;
    
    void main() {
        string x;
    
        x = "First ";
        x += "Second ";
        x += "Third";
    
        cout << x << endl;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member jovton's Avatar
    Join Date
    Nov 2000
    Location
    South Africa
    Posts
    266
    Thanx parksie.
    You seem to know an awf lot.
    But another problem arises.
    I want to put the final result into a LPCTSTR string.
    For example:
    Code:
       string x;
       LPCTSTR MyStr;
       // Do x string stuff
       MyStr = x; // ????????? How now?
    jovton

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You shouldn't use LPCTSTR unless you want to have multiple-character set compatibility. You can access a const pointer to the string by using x.c_str() (a member function):
    Code:
    const char *pStr = x.c_str();
    Or alternatively you can just copy it:
    Code:
    char *pStr = new char[x.length()+1];
    strcpy(pStr, x.c_str);
    pStr[x.length()] = 0;
    Then use pStr as you would normally. For things like MessageBox you can simply pass the result of the function:
    Code:
    MessageBox(hWnd, text.c_str(), caption.c_str(), MB_OK);
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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