Jump to content
  • Advertisement
Sign in to follow this  
GetWindowRect

Easiest String concatenation

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering what the easiest way to concatenate char*'s. I was using lstrcat but it seems it adds a bunch of junk before the string, so when I try to open the file I get errors. I am trying to use mciSendString to open an mp3 player and here is how I am using it.
        char* beg = "open \"";
	char* end = "\" type mpegvideo alias mpeg";
	int iFile = lstrlen(beg) + lstrlen(end) + lstrlen(fileName);
        char* File = new char[iFile];
	lstrcat(File, beg);
	lstrcat(File, fileName);
	lstrcat(File, end);
	MessageBox(NULL, File, "", 0);

fileName is a paramater passed into the function, I tried using + fileName all on one line but I get a "Cannot add two pointers" error. Is there anything that might be easier, or am I stuck trying to figure out how to make strcat work?

Share this post


Link to post
Share on other sites
Advertisement
In plain C or C++?

If it's C++, the std::string has its own concatination which is largely foolproof. In C, you're largely relegated to strcat and its kin.

Share this post


Link to post
Share on other sites
std::string!
std::string File = "open \"";
File += lstrcat(File, fileName);
File += "\" type mpegvideo alias mpeg"
MessageBox(NULL, File.c_str(), "", 0);

edit: beaten

Share this post


Link to post
Share on other sites
I am using c++, but I am trying to get the hang of chars before jumping into strings. Probably a dumb idea, but I want to know how to use char*'s. What is confusing me most is the fact that it adds a bunch of junk to the beginning of the string. The string should only be about 25 characters long or so (guessing) but it is double that and before the string starts is a bunch of ascii which is making it not work.

Share this post


Link to post
Share on other sites
Because char *'s aren't initialized to 0. strcat looks for the first null termination and adds the 2nd char* to it. If the first is garbage, strcat will assume it's just funky characters.

try something like this (edit-note: I prefer to use malloc with char*'s, this I think is not required)

char *begin="foo ";
char *end="bar";
int len=strlen(begin) + strlen(end) + 1; // 1 for the null terminator, rather important

char *foobar=(char *)malloc(len);

strcpy(foobar,begin);
strcat(foobar,end);

// OR

sprintf(foobar,"%s%s",begin,end);



That said, std::string will save you a lot of grief while you get comfortable enough to learn char*'s more easily later.

Share this post


Link to post
Share on other sites
The data within the new char[] allocation doesn't get initialized, so it starts with non-zero chars. So the first lstrcat() call skips over the garbage to the first thing that happens to be null, and starts writing there. This is bad because you're also writing past the array into memory that isn't necessarily yours at all.

Possible solutions:
1) Drop a '\0' into the the first element of the allocated char[] first, so that the first lstrcat() will start there.
2) Use strcpy() (ok, lstrcpy() I guess - I don't know where all these l-prefixes are coming from; what API are you using?) for the first item, so that it sets the contents rather than appending to the garbage.
3) Use the damned std::string already :) It will not just protect you from worrying about initializing data, but also memory management (you did remember to delete[] File afterwards, yes? I'm sure you can imagine situations where it will get very hard to make sure the deletion is done exactly once), string lengths (hint: you still have a bug - you need to allocate one more character for the trailing \0. strlen() doesn't count them; strcat() puts one on at the end so that other string functions can work with them properly.) and all kinds of other nonsense. Life's too short! :)

Share this post


Link to post
Share on other sites
Haha I totally forgot about sprintf... it worked perfectly. I was running into errors deleting the variable because I forgot the +1.

Thanks a bunch for all your help guys.

I didn't know the string class made things so much easier, I guess I will probably start playing around with that instead of char*'s.

Zahlman:
the l prefix to each of the functions is already defined in <windows.h> (I am pretty sure). They work exactly the same as the ones defined in stdio, just need to include an l in front. I also use wsprintf (same as sprint f but defined in windows.h). I don't know how c++ works too much, but I figure the more includes I use the more memory it will be or what not? I just saw in a book I was looking at it used the l prefixed versions without including the other file. Thanks again guys

Share this post


Link to post
Share on other sites
:) It won't use more memory because you include more.. And even if it did, we are talking kb's.. So don't worry about that..


the functions in windows.h.. My first guess would be unicode stuff.. :p

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!