Try the following. Windows does have a StripPath function but including shlwapi.lib just for one function is excessive. Note this is not perfect and there is not error checking (i.e. return tmp+1; is dangerous if tmp is at the end of path) but it’s a start.
Also do not free the return value from strip_path as this is only a pointer into your original string.
// the const means the return value is not yours to free. sorta.
const char *strip_path( char *path )
char *tmp = strrchr( path, (int)’\’ );
if ( tmp == NULL ) return path;
return tmp + 1; // dangerous ... left as an exercise :)
int main( void )
char *path = "C:\mymusic\MP3\mysong.mp3 ";
printf("File is called %s\n", strip_path(path));
Hope this helps.
- ross answered 14 years ago
Please login first to submit.