How to extract filename from path

后端 未结 10 1918
遥遥无期
遥遥无期 2020-12-15 03:26

There should be something elegant in Linux API/POSIX to extract base file name from full path

10条回答
  •  甜味超标
    2020-12-15 03:44

    You could use strstr in case you are interested in the directory names too:

    char *path ="ab/cde/fg.out";
    char *ssc;
    int l = 0;
    ssc = strstr(path, "/");
    do{
        l = strlen(ssc) + 1;
        path = &path[strlen(path)-l+2];
        ssc = strstr(path, "/");
    }while(ssc);
    printf("%s\n", path);
    

提交回复
热议问题