可以说我有一个功能:
void split_path_file(char** p, char** f, char *pf)
{
//malloc and set *p to file path, malloc and set *f to file name
//pf is the pointer to the full file and path "C:\sponge\bob\square.pants"
// edit: leave pf in its origional state
}
Run Code Online (Sandbox Code Playgroud)
什么是实现这一目标的最佳方式?
void split_path_file(char** p, char** f, char *pf) {
char *slash = pf, *next;
while ((next = strpbrk(slash + 1, "\\/"))) slash = next;
if (pf != slash) slash++;
*p = strndup(pf, slash - pf);
*f = strdup(slash);
}
Run Code Online (Sandbox Code Playgroud)
(如果pf == slash,那么没有目录组件.)