Filename from path

    0

    0

    Giovanny Gongora

    Codiga's C++ Recipes

    Get a file name from a Windows path

    #include <string>
    
    int main() {
      std::string filename = "C:\\MyDirectory\\MyFile.bat";
      const size_t last_slash_idx = filename.find_last_of("\\/");
      if (std::string::npos != last_slash_idx) {
        filename.erase(0, last_slash_idx + 1);
      }
      const size_t period_idx = filename.rfind('.');
      if (std::string::npos != period_idx) {
        filename.erase(period_idx);
      }
      return 0;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.