用于将文件路径拆分为路径和文件的功能

use*_*033 2 c string

可以说我有一个功能:

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)

什么是实现这一目标的最佳方式?

eph*_*ent 7

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,那么没有目录组件.)