C++ get file name – How to Get Filename From a Path With or Without Extension in C++

Methods to get filename from a path with or without extension in c++

C++ get file name: In this article, we discuss the different methods to get filename from a path with or without extension in c++. Let’s see c get filename from path with extension, c get filename from path without extension, c remove path from filename, c find all files in directory with extension, c iterate through files in a directory, c filesystempath to string, c++ get filename from path, c get file path, c++ iterate through files in a directory, c++ get file path, c filesystem::path to string, How to get filename from a path with or without extension in c++. The method that we discuss are given below:-

Let’s first understand what we will be going to do in this article. Suppose there is a path name p="/home/user/c++/program.c++" then in this example filename with extension is program.c++ and filename without extension is program. So we will be writing code in c++ to do this task.

Let’s understand all the methods one by one.

Method 1-Using string functions

C++ get file extension: In this method, we use the string in c++ to do the task. We will use different functions that are used in c++ strings to extract the filename. Let see this with the help of an example.

#include <iostream>
#include <string>

using std::string;
string getFileNameWithExtension(const string& s) {
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length());
if (i != string::npos) 
{
string filename = s.substr(i+1, s.length() - i);
string rawname = filename.substr(0, s.length()); 
return(rawname);
}

return("");
}

string getFileNameWithoutExtension(const string& s) {
char sep = '/';
#ifdef _WIN32
sep = '\\';
#endif
size_t i = s.rfind(sep, s.length());
if (i != string::npos) 
{
string filename = s.substr(i+1, s.length() - i);
size_t lastindex = filename.find_last_of("."); 
string rawname = filename.substr(0, lastindex); 
return(rawname);
}

return("");
}

int main() {

string path = "/home/user/c++/program.c++";
string filename_with_extension = getFileNameWithExtension(path);
string filename_without_extension= getFileNameWithoutExtension(path);
std::cout << "The file name with extension  is \"" << filename_with_extension  << "\"\n";
std::cout << "The file name without extension  is \"" << filename_without_extension << "\"\n";
}

Output

The file name with extension is "program.c++"
The file name without extension is "program"

Method 2-Using C++17 Filesystem library

Java get filename without extension: The Filesystem library provides facilities for performing operations on file systems and their components, such as paths, regular files, and directories. So here we will use the Filesystem library to get filename from a path with and without extension. Let’s write code for this.

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
 
int main()
{
    std::cout << "The file name with extension  is " <<fs::path( "/home/user/c++/program.c++" ).filename() << '\n';
    std::cout << "The file name without extension  is " <<fs::path( "/home/user/c++/program.c++" ).stem() << '\n';
        
}

Output

The file name with extension is "program.c++"
The file name without extension is "program"

So these are the methods to get filename with and without extension in c++.