c 2b 2b filename from path

Solutions on MaxInterview for c 2b 2b filename from path by the best coders in the world

showing results for - "c 2b 2b filename from path"
Darby
25 Mar 2019
1// get filename
2std::string base_filename = full_path.substr(full_path.find_last_of("/\\") + 1);
3
4// remove extension from filename
5std::string::size_type const p(base_filename.find_last_of('.'));
6std::string file_without_extension = base_filename.substr(0, p);
Alonso
03 Jul 2017
1// The GetFilenameFromFullpath function does the job
2
3#include <iostream>
4using namespace std;
5
6string GetFilenameFromFullpath(string Filepath, bool IncludeExtension)
7{
8    // Remove folder path
9    string Filename = Filepath.substr(Filepath.find_last_of("/\\") + 1);
10
11    if (IncludeExtension == false)
12    {
13        // Remove extension
14        string Extension = GetFileExtension(Filename);
15        Filename = Filename.substr(0,Filename.length() - Extension.length());
16    }
17    
18    return Filename;
19}
20
21string GetFileExtension(string Filename)
22{
23   size_t LastDot = Filename.find_last_of(".");
24
25   if (LastDot == string::npos)
26   {
27      return ""; // No extension in Filename string
28   }
29
30   return Filename.substr(LastDot);
31}