function to split a filepath into path and file

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

    Maybe a bit late to the party, but the best solution I have found and have been using for years is the two functions dirname and basename

    path         dirname    basename
    "/usr/lib"    "/usr"    "lib"
    "/usr/"       "/"       "usr"
    "usr"         "."       "usr"
    "/"           "/"       "/"
    

    They are great for separating parts of a path/filename. Together with realpath() they are IMHO unbeatable in simplicity and power.

    http://linux.die.net/man/3/basename

    http://man7.org/linux/man-pages/man3/realpath.3.html

提交回复
热议问题