How to extract filename from path

后端 未结 10 1895
遥遥无期
遥遥无期 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:37

    template
    charType* getFileNameFromPath( charType* path )
    {
        if( path == NULL )
            return NULL;
    
        charType * pFileName = path;
        for( charType * pCur = path; *pCur != '\0'; pCur++)
        {
            if( *pCur == '/' || *pCur == '\\' )
                pFileName = pCur+1;
        }
        
        return pFileName;
    }
    

    call: wchar_t * fileName = getFileNameFromPath < wchar_t > ( filePath );

    (this is a c++)

提交回复
热议问题