Table of Contents
This article explains the various ways to get filename from path using C++ programs. The examples will also describe ways to remove extensions as well if such needs arise.
Get Filename From Path in C++
Finding a file name from its file path is a simple task. The program needs to find the last delimiter, and delete everything that occurs before it.
If extensions are needed to be removed, the program searches for a period(.) and deletes everything that occurs after that. Let’s look at some examples to understand how to get filename from path.
Using find_last_of and substr methods
To get filename from path, the program uses two methods – clear_slash()
to find delimiters and extension_removal()
to remove the extensions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <string> #include<iostream> using namespace std; string clear_slash(string const& path_of_file, string const& d_slash = "/\\") { size_t index_of_slash = path_of_file.find_last_of(d_slash); string file_name = path_of_file.substr(index_of_slash + 1); return file_name; } string extension_removal(string const& name_of_file) { size_t index_of_period = name_of_file.find_last_of('.'); string file_without_extension = name_of_file.substr(0, index_of_period); return file_without_extension; } int main() { string file_address = "C:\\Users\\Win 10\\Documents\\get_filename.md"; cout << "Filename With extension = " <<clear_slash(file_address) << endl; cout << "Just the Filename = " << extension_removal(clear_slash(file_address)) << endl; } |
Output:
Just the Filename = get_filename
Let’s break down the code
The first two lines of code import the dependencies of the program – library string
and iostream
. In the third line, the scope of std
is included, which means datatypes like string
can be used without using any prefix like std::string
.
Method clear_slash():
1 2 3 4 5 6 7 8 |
string clear_slash(string const& path_of_file, string const& d_slash = "/\\") { size_t index_of_slash = path_of_file.find_last_of(d_slash); string file_name = path_of_file.substr(index_of_slash + 1); return file_name; } |
A string method clear_slash
is created with two string parameters – path_of_file
which stores the file path and d_slash
. The variable d_slash
is initialized with the value '/\\'
which means double slash.
The method separates the file name from the file path. It does it by finding the last delimiter – \\
from the file path and removing the string before it.
To find the index of the last delimiter, the function find_last_of()
is used. The syntax path_of_file.find_last_of(d_slash)
takes the value from d_slash
and searches the last occurrence of it in variable path_of_file
. It returns the index of the last double slashes, and it is stored inside the variable index_of_slash
of datatype – size_t
.
The substr()
function returns substring from a given string. The syntax path_of_file.substr(index_of_slash + 1)
returns the string that are after the index of \\
from the variable path_of_file
and stores it inside a new string variable file_name
.
Lastly, the variable file_name
is returned.
Method extension_removal():
The second method is used to remove the extension from the file path. A string method extension_removal
having a string variable name_of_file
as the parameter will get the file name passed to this method and it will return the filename without extension.
1 2 3 4 5 6 7 8 |
string extension_removal(string const& name_of_file) { size_t index_of_period = name_of_file.find_last_of('.'); string file_without_extension = name_of_file.substr(0, index_of_period); return file_without_extension; } |
Removing extensions is similar to removing delimiters. First, a variable index_of_period
of datatype size_t
is created which stores the index of period(.) searched from the name_of_file
using the function find_last_of()
.
The string available before the period(.) is saved inside a new string variable file_without_extension
using the substr
function, starting from the 0th
index to index_of_period
.
Finally, the variable file_without_extension
is returned. Now, we will look at how to use these two methods to get filename from path.
main_function:
1 2 3 4 5 6 7 |
int main() { string file_address = "C:\\Users\\Win 10\\Documents\\get_filename.md"; cout << "Filename With extension = " <<clear_slash(file_address) << endl; cout << "Just the Filename = " << extension_removal(clear_slash(file_address)) << endl; } |
The path of the file is stored inside the variable file_address
. In the second line, the method clear_slash()
uses the file_address
to pass the file path, separate the path, and then print the returned value.
To remove extension as well from the file path, both the methods are nested and called with file_address
passed as an argument. It returns just the filename from the file path and it is printed.
Using Templates
A template is used to get filename from path when different instances of string are used, like – string
or wstring
(strings with a wider sequence than basic strings).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <string> #include<iostream> using namespace std; template<class T> T remove_slash(T const& path_of_file, T const& d_slash = "/\\") { size_t index_of_slash = path_of_file.find_last_of(d_slash); string file_name = path_of_file.substr(index_of_slash + 1); return file_name; } template<class T> T extension_removal(T const& name_of_file) { typename T::size_type const period_index(name_of_file.find_last_of('.')); string filename = name_of_file.substr(0, period_index); return filename; } int main() { string filepath1 = remove_slash<std::string>("C:\\Users\\Win 10\\Documents\\get_filename.md"); string filepath2 = extension_removal<std::string>("C:\\Users\\Win 10\\Documents\\get_filename.md"); cout << remove_slash(filepath1) << endl; cout << extension_removal(filepath2) << endl; cout << extension_removal(remove_slash(filepath1)) << endl; } |
Output:
C:\Users\Win 10\Documents\get_filename
get_filename
Import packages are the same as the last program.
A template class T
is defined. The first method remove_slash
is created with T
as a datatype. It has parameters – path_of_file
and d_slash
with datatype T
.
The second method extension_removal
created with datatype T
has parameter name_of_file
.
Inside the main function, the string variable filepath1
stores the address of the file with remove_slash
as its identifier. Similarly, filepath2
stores address with extension_removal
as its identifier.
The first method remove_slash
is called with filepath1
passed as its argument and the returned value is printed. Then extension_removal
is called with filepath2
sent as its argument. In nested methods, both file path variables can be used.
This example can be used with other specialized string types as well like wstring
, etc to get filename from path.
Further reading:
Using filesysystem library [ C++ 17 ]
If the system has C++ 17 or later, the easiest way is to use the filesystem
library to get filename from path.
The filesystem
library is assigned to a keyword obj
using the namespace
.
To get the file name, two functions are used:
filename()
– Returns the filename with extension.stem()
– Returns just the filename.
Let’s assume a variable filename
has a file path like – "C:\\MyDirectory\\MyFile.bat"
. To get the filename from path, the following syntax is used:
1 2 3 4 |
std::string filename = "C:\\MyDirectory\\MyFile.bat"; std::cout << fs::path(filename).filename() << '\n' |
This will return – "MyFile.bat"
. To remove extensions as well, use the command:
1 2 3 |
std::cout << fs::path(filename).stem(); |
This will return – "MyFile"
The below program has three imports – iostream
, string
, and the filesystem
library and it uses a list of inputs to find out what the filesystem
functions return for specific inputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <filesystem> namespace obj = std::filesystem; using namespace std; int main() { //method 1 string file_address = "C:\\Users\\Win 10\\Pictures\\pic1.jpg"; cout << obj::path(file_address).filename() << "\n" << endl; cout << obj::path(file_address).stem() << "\n" << endl; //method 2 cout << obj::path("C:/Users/Win 10/AppData/app.exe").filename() << "\n" << endl; cout << obj::path("C:/Users/Win 10/AppData/app.exe").stem() << "\n" << endl; } |
Output:
“pic1”
“app.exe”
“app”
Conclusion
This program provides a brief description of the various ways to get filename from path using C++. The reader will be able to use the concepts easily after reading the article.