function to split a filepath into path and file

前端 未结 6 1538
猫巷女王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:51

    I came up with the following, of course this assumes that pf is malloced.

    void split_path_file(char** p, char **f, char *pf)
    {
        char *posp = strrchr(pf, '\\');
        *posp = '\0';
        *p = strdup(pf);
        *f = strdup(posp+1);
        *posp = '\\';
    }
    

    Not sure if this is a better approach than above answers.

提交回复
热议问题