C++: How to get filename from a path with or without extension | Boost | C++17 FileSytem Library
In this article we will discuss different ways to get filename from a path with or without extension using,
- Boost Filesystem Library
- C++17 Filesystem Library
- Prior to c++11 Days
Suppose we have a file path i.e.
1 |
"/home/user/sample/temp/data.csv" |
We want to fetch file name from this path i.e. “data.csv” and “data” (With or without extension).
Let’s see how to do that,
Get FileName using Boost & C++17 FileSystem Library
Both Boost & C++17 FileSystem Library provides similar API under different name spaces.
Let’s see how to do that,
Create Path object from given string i.e.
1 2 |
// Create a Path object from File Path filesys::path pathObj(filePath); |
Then check if given path has a stem and if yes then get that stem.
1 2 3 4 5 6 7 |
// Check if file has stem i.e. filename without extension if(pathObj.has_stem()) { // return the stem (file name without extension) from path object return pathObj.stem().string(); } return ""; |
stem here represents the filename with extension.
To get the file name without extension all the filename() function on path i.e.
1 |
pathObj.filename().string(); |
Checkout complete function as follows,
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 |
/* * Get File Name from a Path with or without extension */ std::string getFileName(std::string filePath, bool withExtension = true) { // Create a Path object from File Path filesys::path pathObj(filePath); // Check if file name is required without extension if(withExtension == false) { // Check if file has stem i.e. filename without extension if(pathObj.has_stem()) { // return the stem (file name without extension) from path object return pathObj.stem().string(); } return ""; } else { // return the file name with extension from path object return pathObj.filename().string(); } } |
To use the above function with Boost File System Library, use following header file and namesapce,
1 2 3 |
#include <boost/filesystem.hpp> namespace filesys = boost::filesystem; |
To use the above function with C++17 FileSystem Library, use following header file and namesapce,
1 2 3 |
#include <experimental/filesystem> namespace filesys = std::experimental::filesystem; |
Complete executable example using Boost is as follows,
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <iostream> #include <cassert> #include <string> #include <boost/filesystem.hpp> namespace filesys = boost::filesystem; /* * Get File Name from a Path with or without extension */ std::string getFileName(std::string filePath, bool withExtension = true) { // Create a Path object from File Path filesys::path pathObj(filePath); // Check if file name is required without extension if(withExtension == false) { // Check if file has stem i.e. filename without extension if(pathObj.has_stem()) { // return the stem (file name without extension) from path object return pathObj.stem().string(); } return ""; } else { // return the file name with extension from path object return pathObj.filename().string(); } } int main() { std::string filePath = "/home/user/sample/temp/data.csv"; // Get File name with extension from file path std::string name = getFileName(filePath); assert(name == "data.csv"); std::cout<<name<<std::endl; // Get File name with extension from file path name = getFileName(filePath, false); assert(name == "data"); std::cout<<name<<std::endl; return 0; } |
Output:
1 2 |
data.csv data |
To compile the above code in linux using Boost Filesystem Library, use following command,
1 |
g++ -std=c++11 example.cpp -lboost_filesystem -lboost_system |
To compile the above code in C++17, change the header file and namespace as mentioned above and use following command,
1 |
g++ --std=c++17 example.cpp -lstdc++fs |
Get File Name using C++ std::string functions
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 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> #include <cassert> #include <string> /* * Get File Name from a Path with or without extension */ std::string getFileName(std::string filePath, bool withExtension = true, char seperator = '/') { // Get last dot position std::size_t dotPos = filePath.rfind('.'); std::size_t sepPos = filePath.rfind(seperator); if(sepPos != std::string::npos) { return filePath.substr(sepPos + 1, filePath.size() - (withExtension || dotPos != std::string::npos ? 1 : dotPos) ); } return ""; } int main() { std::string filePath = "/home/user/sample/temp/data.csv"; // Get File name with extension from file path std::string name = getFileName(filePath); assert(name == "data.csv"); std::cout<<name<<std::endl; // Get File name with extension from file path name = getFileName(filePath, false); std::cout<<name<<std::endl; assert(name == "data.csv"); std::cout<<name<<std::endl; return 0; } |
Output:
1 2 3 |
data.csv data.csv data.csv |
Follow @thisPtr
Leave a Reply