Get a file name from a path

前端 未结 21 1637
面向向阳花
面向向阳花 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条回答
  •  旧时难觅i
    2020-12-01 00:17

    C++11 variant (inspired by James Kanze's version) with uniform initialization and anonymous inline lambda.

    std::string basename(const std::string& pathname)
    {
        return {std::find_if(pathname.rbegin(), pathname.rend(),
                             [](char c) { return c == '/'; }).base(),
                pathname.end()};
    }
    

    It does not remove the file extension though.

提交回复
热议问题