Results 1 to 7 of 7

Thread: Combine two (or more) strings

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242

    Arrow

    Hello!

    It's me again and now I have another question for one of you.
    How can I combine 2 (or more) strings (like in VB: str1 = str2 & str3)?
    I tried with + operator but I allways get an error: "error C2110: cannot add two pointers"
    Here's the code:
    Code:
    LPTSTR tcCurDir;
    	GetCurrentDirectory(MAX_PATH, tcCurDir);
    
    	cCurDir = tcCurDir + "\\bla.exe";
    	
    	ShellExecute(NULL, "open", tcCurDir, NULL,NULL,SW_SHOWDEFAULT);
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Damn, I got a BUG!
    The right code is this one:
    Code:
    LPTSTR tcCurDir;
    GetCurrentDirectory(MAX_PATH, tcCurDir);
    
    tcCurDir = tcCurDir + "\\bla.exe";
    	
    ShellExecute(NULL, "open", tcCurDir, NULL,NULL,SW_SHOWDEFAULT);
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    I think this will work.
    Code:
    TCHAR curdir[256];
    
    GetCurrentDirectory(MAX_PATH,curdir);
    char fl[9]={"\\bla.exe"};
    strcat(curdir,fl);
    The Path and filename You want is stored in curdir.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Nope. You can't. In C++, your basic string isn't a string. It's an array, terminated with a '\0'.
    To add two strings, you make another buffer that's big enough for both, then copy them in. Also, you need to use the _T macro:
    Code:
    TCHAR ptcCurDir[MAX_PATH];
    TCHAR *ptcFile = _T("\\blah.exe");
    GetCurrentDirectory(MAX_PATH, ptcCurDir);
    
    ptcFile = new TCHAR[_tcslen(ptcCurDir) + _tcslen(ptcFile) + 1];
    
    _tcscpy(ptcFile, ptcCurDir);
    _tcscpy(ptcFile+_tcslen(ptcCurDir), ptcFile);
    ptcFile[_tcslen(ptcCurDir) + _tcslen(ptcFile)] = 0;
    ...or similar. I can't test this under NT, but that's the basic idea.
    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

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Um...Vlatko - you've got your Unicode stuff in a twist. You can't mix & match like that...

    Zvonko - you can actually get round this by using the STL string class (file string must be included, and a using namespace std; somewhere in your headers)
    Code:
    TCHAR ptcCurDir[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, ptcCurDir);
    basic_string<TCHAR> sFinalPath = ptcCurDir;
    sFinalPath += _T("\\blah.exe");
    ...pass sFinalPath.c_str() in place of an LPTSTR argument.
    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Wow, guys!

    Don't know for your opinion, but you RULE!

    Where do you get this much knowledge?

    I have some (actually pretty much) knowledge about VB - I'm programming with it now over 2 years.
    I know that I've just started VC++ and I don't know much about programming in it, but it seems you have all that knowledge in your little finger.
    As I already told you I'm rookie (very green rookie) and don't resend me if I'll be asking stupid questions...

    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    Where do you get this much knowledge?
    ...how does doing it for 8 years sound?

    I don't resent people who know less than me, because I was once in that stage, and needing help myself.

    Seriously, though, once you've got pointers, arrays, and data type sizes well and truly sorted, you've arrived .
    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