Get a file name from a path

前端 未结 21 1635
面向向阳花
面向向阳花 2020-11-30 23:55

What is the simplest way to get the file name that from a path?

string filename = \"C:\\\\MyDirectory\\\\MyFile.bat\"

In this example, I s

21条回答
  •  误落风尘
    2020-12-01 00:30

    This should work too :

    // strPath = "C:\\Dir\\File.bat" for example
    std::string getFileName(const std::string& strPath)
    {
        size_t iLastSeparator = 0;
        return strPath.substr((iLastSeparator = strPath.find_last_of("\\")) != std::string::npos ? iLastSeparator + 1 : 0, strPath.size() - strPath.find_last_of("."));
    }
    

    If you can use it, Qt provide QString (with split, trim etc), QFile, QPath, QFileInfo etc to manipulate files, filenames and directories. And of course it's also cross plaftorm.

提交回复
热议问题