Answers for "c++ get filename from path"

C++
2

c++ get filename from path

// get filename
std::string base_filename = full_path.substr(full_path.find_last_of("/\\") + 1);

// remove extension from filename
std::string::size_type const p(base_filename.find_last_of('.'));
std::string file_without_extension = base_filename.substr(0, p);
Posted by: Guest on April-15-2021
0

c++ filename from path

// The GetFilenameFromFullpath function does the job

#include <iostream>
using namespace std;

string GetFilenameFromFullpath(string Filepath, bool IncludeExtension)
{
    // Remove folder path
    string Filename = Filepath.substr(Filepath.find_last_of("/\\") + 1);

    if (IncludeExtension == false)
    {
        // Remove extension
        string Extension = GetFileExtension(Filename);
        Filename = Filename.substr(0,Filename.length() - Extension.length());
    }
    
    return Filename;
}

string GetFileExtension(string Filename)
{
   size_t LastDot = Filename.find_last_of(".");

   if (LastDot == string::npos)
   {
      return ""; // No extension in Filename string
   }

   return Filename.substr(LastDot);
}
Posted by: Guest on July-16-2021

Code answers related to "c++ get filename from path"

Browse Popular Code Answers by Language