I want to throw the last three character from file name and get the rest?
I have this code:
char* remove(char* mystr) {
char tmp[] = {0};
unsigned int x;
for (x = 0; x < (strlen(mystr) - 3); x++)
tmp[x] = mystr[x];
return tmp;
}
Try:
You’ll have to free the returned string yourself. It simply finds the last
.in the string and replaces it with a null terminator character. It will handle errors (passingNULLor running out of memory) by returningNULL.It won’t work with things like
/this.path/is_badsince it will find the.in the non-file portion but you could handle this by also doing astrrchrof/, or whatever your path separator is, and ensuring it’s position isNULLor before the.position.A more general purpose solution to this problem could be:
and this produces: