-2
可能重複:
function to split a filepath into path and file分離路徑分爲兩個部分
我要像 「A/B/C /目錄/文件名」 路徑名分成「A/B/c /目錄「和」文件名「。在C中做這件事的好方法是什麼?
可能重複:
function to split a filepath into path and file分離路徑分爲兩個部分
我要像 「A/B/C /目錄/文件名」 路徑名分成「A/B/c /目錄「和」文件名「。在C中做這件事的好方法是什麼?
#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);
}