-
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. :)
-
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;
}
-
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?
-
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);