Why isn''t
Astring = "resource\\grass.bmp";
equal to
Astring = strcat("resource\\","grass.bmp");
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
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
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?
LoadImage(strcat("Resource\\", Tile[2]));
//Tile[] is the array of filenames
But this doesn''t work. Why not and how do I fix?
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:
Alternatively, you could use STL strings:
The only restriction you''ll have using STL string is that its c_str() method returns a const char *.
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 stringstrcat(str, Tile[2]);LoadImage(str);
Alternatively, you could use STL strings:
#include <string> //str = "Resource\\";str.append(Tile[2]);LoadImage(str.c_str());
The only restriction you''ll have using STL string is that its c_str() method returns a const char *.
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]);
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]);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement