Concatenation of two LPSTR
-
Well, I have my string, let's say
LPSTR myString = "up?";
I'd want to add " Whats " in front of it, just likeLPSTR myString = "up?"; myString = "Whats " + myString;
I get an error. Then I tried doing:LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString);
This also doesn't work. Any help please? Thanks ~Mike -
Well, I have my string, let's say
LPSTR myString = "up?";
I'd want to add " Whats " in front of it, just likeLPSTR myString = "up?"; myString = "Whats " + myString;
I get an error. Then I tried doing:LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString);
This also doesn't work. Any help please? Thanks ~MikeYou have to understand that an LPSTR is a very primitive thing: a pointer to an array of characters that is supposed to have a \0 on the end. You can do operations with LPSTR (look at the strcat function) but you have to make sure that you have allocated another array to hold the result that is large enough. Manipulating strings this way has been done for years in C and has led to innumerable buffer overflow errors. You should never do it in C++. You should use the string classes (std::string, std::wstring for wide characters) that are in the standard library, which take care of buffer allocation (and many other useful things) for you. You can concatenate std::string with the overloaded + operator and get just what you expect.
-
Well, I have my string, let's say
LPSTR myString = "up?";
I'd want to add " Whats " in front of it, just likeLPSTR myString = "up?"; myString = "Whats " + myString;
I get an error. Then I tried doing:LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString);
This also doesn't work. Any help please? Thanks ~Mikethis seemed to work: #include #include #include LPSTR concat(LPSTR s1, LPSTR s2) { char* str = new char[strlen(s1) + strlen(s2) + 1]; LPSTR final_str = str; strcpy(str, s1); strcat(str, s2); return final_str; } int main() { LPSTR s1 = "this is a"; LPSTR s2 = " test."; cout << concat(s1, s2) << endl; return 0; }
-
Well, I have my string, let's say
LPSTR myString = "up?";
I'd want to add " Whats " in front of it, just likeLPSTR myString = "up?"; myString = "Whats " + myString;
I get an error. Then I tried doing:LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString);
This also doesn't work. Any help please? Thanks ~MikeQuick answer: C-style strings don't work like that. Long answer: See the above replies for some code that does what you want, although you must learn how C-style strings work in order to use the
strcat
/strcpy
functions properly. Thestd::string
class is much easier to learn and use if you don't absolutely need a C-style string. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Actual sign at the laundromat I go to: "No tinting or dying." -
Well, I have my string, let's say
LPSTR myString = "up?";
I'd want to add " Whats " in front of it, just likeLPSTR myString = "up?"; myString = "Whats " + myString;
I get an error. Then I tried doing:LPSTR myString = "up?"; sprintf(myString,"Whats %s",myString);
This also doesn't work. Any help please? Thanks ~Mikeit is wrong certainly.because your "myString" points to a static data area,which can not be changed.so ,your "myString = "Whats " + myString" is wrong.you must do so: char str[10]="up?"; char mystring[20]; sprintf(mystring,"Whats %s",str);