Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.



This topic is 6197 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''ll assume u are not using MFC (as i don''t know it)
If you are wanting to put "resource\grass.bmp" into Astring, you would want to do: strcpy(Astring, "resource\\grass.bmp");
if you are wanting to take the 2 strings, and put them into one, i would use:
strcpy(Astring, "resource\\");
strcat(Astring, "grass.bmp");
anyway, strcat takes 2 parameters, both of those are char pointers (char *), both of the strings in your strcat are constants, so i don''t think either would work in this situation

Share this post

Link to post
Share on other sites
Ok here''s the exact problem, I''m making a game engine and all the names of my image filenames are stored in an array and I have a function called LoadImage(char *Filename) that takes the filename of the tile, loads to surface, etc. Now I have all my tiles in a Resource folder so I want to go:

LoadImage(strcat("Resource\\", Tile[2]));

//Tile[] is the array of filenames

But this doesn''t work. Why not and how do I fix?

Share this post

Link to post
Share on other sites
It doesn''t work because the arguments to strcat() - look it up - are the destination and source strings. It doesn''t return the concatenated string.

How do you solve it? You cat the strings, then you use the result as your parameter:

char *str;
strcpy(str, "Resource\\", strlen("Resource\\")); // you have to pass the length of the string
strcat(str, Tile[2]);

Alternatively, you could use STL strings:

#include <string>
str = "Resource\\";

The only restriction you''ll have using STL string is that its c_str() method returns a const char *.

Share this post

Link to post
Share on other sites
The declaration for strcat is:

char *strcat( char *strDestination, const char *strSource );

So, the first pointer you specify is the memory space where the combined strings will be placed.

You''re passing in a string constant as the first argument, which only has exactly enough space allocated for the string constant. If you cat something onto the end of it, it will start writing over other strings or other random memory. Really, the string should be a const char * and not even let you make the call.

You''d be better off allocating a small character array on the stack (char filename[256]) and using sprintf to create the filename:

sprintf(filename, "resource\\%s", temp[2]);

Share this post

Link to post
Share on other sites

  • 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!