PDA

Click to See Complete Forum and Search --> : Combine two (or more) strings


Zvonko
Sep 17th, 2000, 10:30 AM
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:
LPTSTR tcCurDir;
GetCurrentDirectory(MAX_PATH, tcCurDir);

cCurDir = tcCurDir + "\\bla.exe";

ShellExecute(NULL, "open", tcCurDir, NULL,NULL,SW_SHOWDEFAULT);

Zvonko
Sep 17th, 2000, 10:32 AM
Damn, I got a BUG!
The right code is this one:
LPTSTR tcCurDir;
GetCurrentDirectory(MAX_PATH, tcCurDir);

tcCurDir = tcCurDir + "\\bla.exe";

ShellExecute(NULL, "open", tcCurDir, NULL,NULL,SW_SHOWDEFAULT);

Vlatko
Sep 17th, 2000, 10:46 AM
I think this will work.

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.

parksie
Sep 17th, 2000, 10:48 AM
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:

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.

parksie
Sep 17th, 2000, 11:30 AM
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)

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.

Zvonko
Sep 17th, 2000, 02:13 PM
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... :)

parksie
Sep 17th, 2000, 03:15 PM
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 :).