How to extract filename from path

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

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

10条回答
  •  萌比男神i
    2020-12-15 03:48

    My example (improved):

        #include 
    
        const char* getFileNameFromPath(const char* path, char separator = '/')
        {
            if(path != nullptr)
            {
                for(size_t i = strlen(path);  i > 0; --i)
                {
                    if (path[i-1] == separator)
                    {
                        return &path[i];
                    }
                }
            }
        
            return path;
        }
    

提交回复
热议问题