# Making part of a directory a variable

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

## Recommended Posts

void LoadCharacter(struct Player* p,char* dir) {

}


im trying to make a directory a variable.. i've tried every combination.the current one thats in there compiles but it cant find the image. obviously its not right. im also not even sure if its possible the function call just looks like LoadCharacter(&player,"player");

##### Share on other sites
are you using C++? if so, you should be using std::strings. in which case its trivial to concatanate 2 strings. it would look like this:

{
}

i think thats what your trying to do, right? basically, you want to send the function "player", but you want to send load_pcx "player/stand1.pcx" ??

also, you can use char*'s anyway if you want (i would recommend strongly to use a std::string though). the function you want is called strcat(). it would look something like this (havent used it, so it might not be totally right, but im pretty sure it is...)

{
strcat("/stand1.pcx",dir);
}

something like that... i might have the order of the parameters backwards, just put "strcat" into google..

##### Share on other sites
no im not useing c++...look how i referenced to the player structure in the parameters..also note though load_pcx is only a 2 parameter function. its not working right now..but i'll keep trying.
edit.. i've been messing with it for a while..cant seem to get it

[Edited by - Arch_DOA on October 18, 2004 8:54:15 PM]

##### Share on other sites
It sounds like what you're trying to do is append the "/stand1.pcx" bit to the end of the 'dir' value supplied to the function... the description "make a directory a variable" was completely nonsensical, so I'm guessing. :/

Anyway, in that case, strcat() is what you want; but you have to be sure there is available memory (that belongs to you) where the extra bit will be written.

This should be right:
const char* filename = "stand1.pcx";void LoadCharacter(struct Player* p,char* dir) {  // Find the length of dir, and allocate a temporary buffer big  // enough to hold that string and the bit to append.  // We have to do this because the bit of memory right after the  // end of 'dir' might not belong to us, so writing the  // "/stand1.pcx" there could cause a segfault.  char * buf = malloc(strlen(dir) + strlen(filename) + 1);  // We need enough bytes for both strings, plus a null   // terminator. 'strlen' does not count the null terminator  // when it finds the length.  // Next, put "dir" at the beginning of the buffer.  strcpy(buf, dir);  // Now put "filename" at the end of that.  strcat(buf, filename);  // Make our call.  p->img = load_pcx(buf,0);  // Since we allocated some memory that was just for temporary  // use here, we have to dispose of it.  free(buf);}

This stuff really is easier to get right in C++ though :/