我想将“a / b / c / directory / filename”之类的路径名拆分为“a / b / c / directory”和“filename”。在C中这样做的好方法是什么?
答案 0 :(得分:0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char path[] ="/aaa/bbb/ccc/file";
char *part1 = (char *)malloc (strlen(path));
strcpy (part1, path);
char *pos = strrchr (part1, '/');
*pos = '\0';
char *part2 = strdup (pos + 1);
printf ("%s \n%s", part1, part2 );
}