How to get file extension from string in C++

前端 未结 25 2521
迷失自我
迷失自我 2020-11-30 22:35

Given a string \"filename.conf\", how to I verify the extension part?

I need a cross platform solution.

25条回答
  •  一个人的身影
    2020-11-30 23:20

    Is this too simple of a solution?

    #include 
    #include 
    
    int main()
    {
      std::string fn = "filename.conf";
      if(fn.substr(fn.find_last_of(".") + 1) == "conf") {
        std::cout << "Yes..." << std::endl;
      } else {
        std::cout << "No..." << std::endl;
      }
    }
    

提交回复
热议问题