function to split a filepath into path and file

前端 未结 6 1516
猫巷女王i
猫巷女王i 2020-12-11 11:10

Lets say I have a function:

void split_path_file(char** p, char** f, char *pf)
{
    //malloc and set *p to file path, malloc and set *f to file name
    //p         


        
6条回答
  •  醉梦人生
    2020-12-11 11:57

    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);
    }
    

    (If pf == slash, then there is no directory component.)

提交回复
热议问题