I need to remove the last part of a file path.
For example, if I have this filename "user/doc/file1"
, I want to be able to get "user/doc/"
.
I need to remove the last part of a file path.
For example, if I have this filename "user/doc/file1"
, I want to be able to get "user/doc/"
.
You probably want this:
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "/user/doc/file1";
// find pointer to last '/' in string
char *lastslash = strrchr(string, '/');
if (lastslash) // if found
*(lastslash + 1) = 0; // terminate the string right after the '/'
printf ("string = %s\n", string);
}
Output
string = /user/doc/