Quote:
Originally Posted by kellinwood
Code:
int last_slash_pos = rindex[/b]( path, '/')
|
No. rindex() does not return int, but char*.
Also, strrchr() does the same thing but is better supported along different unix-like OS's.
If you are not changing the string
path or
filename you can just do:
Code:
#include <string.h>
#include <stdio.h>
int main()
{
char *path;
char *filename;
path = "/var/www/index.html";
filename = strrchr(path, '/') + 1;
printf("Filename: %s\n", filename);
return 0;
}
Only if you do need to change the string
path or
filename, you can duplicate the string
filename with strdup(). In that case, don't forget to free() the dynamically allocated memory.
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *path;
char *filename;
path = "/var/www/index.html";
filename = strdup(strrchr(path, '/') + 1);
printf("Filename: %s\n", filename);
free(filename);
return 0;
}