Making part of a directory a variable

Started by
2 comments, last by Zahlman 19 years, 6 months ago

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

	p->img = load_pcx(dir,"/stand1.pcx",0);
}

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");
Advertisement
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:


void LoadCharacter(struct Player* p,std::string dir)
{
p->img = load_pcx(dir,dir + "/stand1.pcx",0);
}

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...)

void LoadCharacter(struct Player* p,char* dir)
{
strcat("/stand1.pcx",dir);
p->img = load_pcx(dir,dir,0);
}

something like that... i might have the order of the parameters backwards, just put "strcat" into google..
FTA, my 2D futuristic action MMORPG
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]
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 :/

This topic is closed to new replies.

Advertisement