Archived

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

ploiu

remove part or char *...

Recommended Posts

ploiu    122
Hi, I have this char* = , for instance, "texture.tga". How do I remove, using C/C++, the ".tga", from it, and return only "texture" ? Thanks

Share this post


Link to post
Share on other sites
kaysik    382
a char* is an array so you can loop it like an array. Something like :

for (int j = (strlen(myStr) - 1); j >= 0; --j)
if (myStr[j] == '.')
{
myStr[j] = '\0';
break;
}

of course i wrote that off the top of my head so there is bound to be some random error in there but you get the idea. If you replace the '.' with the null char '\0' then the string will end there so .tga will dissapear

pto

(NOTE : found one random error already )

[edited by - kaysik on July 6, 2003 5:49:48 AM]

Share this post


Link to post
Share on other sites
brassfish89    122
It depends how it is declared. If it is like this:
char* texture="texture.tga";
Then no you can''t do that since the above is the exact same as this:
const char* texture="texture.tga";
However, if you declare it like this:
char texture[]="texture.tga";
Then kaysik''s method will work.



-If you see this image I may or may not be online
My website

Share this post


Link to post
Share on other sites
Way Walker    745
Using C (I'm not familiar with C++) the simplest way would be

*strrchr(myString, '.') = '\0';  


Assuming you did one of the following

char myString[] = "texture.tga"; 


or


char *myString = malloc(strlen("texture.tga") + 1);
strcpy(myString, "texture.tga");


As mentioned before,
char *myString = "texture.tga";  
is much like
const char *myString = "texture.tga";  
except that it'll only fail at runtime on some implementations (but not necessarily other implementations).


EDIT: Fix tags and realize that my C++ idea doesn't work

[edited by - Way Walker on July 6, 2003 3:21:33 PM]

Share this post


Link to post
Share on other sites
Illumini    205
C++ standard library would do something like this


int ext;
string myString = "texture.tga";
if ( (ext = myString.find(''.'')) != string::npos)
myString = myString.substr(0, ext);

Share this post


Link to post
Share on other sites