char *path = ... /* fill this however you like, for example function argument */
char *AAA_start, *AAA_end;
char *BBB_start, *BBB_end;
// go the end of the string and find the first .
for (BBB_end = path+strlen(path); *BBB_end != '.'; --BBB_end);
// continue to find the first/
for (BBB_start = BBB_end; *BBB_start != '/'; --BBB_start);
// Now you have the beginning and end of BBB
// continue from there to find next/
for (AAA_end = BBB_start-1; *AAA_end != '/'; --AAA_end);
// continue from there to find next/
for (AAA_start = AAA_end-1; *AAA_start != '/'; --AAA_start);
// Now you have the beginning and end of AAA
// Now you can do whatever you want with AAA and BBB, for example
char *AAA = new char[AAA_end-AAA_start+2]; // AAA_end is included in the result
// hence +1. Another +1 for the NULL
char *BBB = new char[BBB_end-BBB_start+2];
memcpy(AAA, AAA_start, AAA_end-AAA_start+1);
memcpy(BBB, BBB_start, BBB_end-BBB_start+1);
AAA[AAA_end-AAA_start+1] = NULL;
BBB[BBB_end-BBB_start+1] = NULL;
这是基本的想法。现在,你需要添加错误检查到这一点:
char *path = ... /* fill this however you like, for example function argument */
char *AAA_start, *AAA_end;
char *BBB_start, *BBB_end;
for (BBB_end = path+strlen(path); *BBB_end != '.' && BBB_end != path; --BBB_end);
if (BBB_end == path) return FAIL;
for (BBB_start = BBB_end; *BBB_start != '/' && BBB_start != path; --BBB_start);
if (BBB_start == path) return FAIL;
for (AAA_end = BBB_start-1; *AAA_end != '/' && AAA_end != path; --AAA_end);
if (AAA_end == path) return FAIL;
for (AAA_start = AAA_end-1; *AAA_start != '/' && AAA_start != path; --AAA_start);
if (AAA_start == path && *AAA_start != '/') return FAIL;
char *AAA = new char[AAA_end-AAA_start+2];
char *BBB = new char[BBB_end-BBB_start+2];
memcpy(AAA, AAA_start, AAA_end-AAA_start+1);
memcpy(BBB, BBB_start, BBB_end-BBB_start+1);
AAA[AAA_end-AAA_start+1] = NULL;
BBB[BBB_end-BBB_start+1] = NULL;
您是否尝试过使用['strtok()'](http://www.cplusplus.com/reference/clibrary/cstring/strtok/)? – Kusalananda
@KAK'strtok'是我尽可能避免它的最有缺陷的c函数之一。 – CodesInChaos
@CodeInChaos,为什么? – Kusalananda