C++: How to extract file extension from a path string using Boost & C++17 FileSystem Library
In this article we will dicuss different ways to extract extension from a given path string using different technique i.e.
- C++17 FileSystem Library
- Boost FileSystem Library
- C++ STL
Suppose given file is,
1 |
/home/user/sample/temp/logs.out |
It’s extension should be .out.
Fetch extension of a given file using Boost Filesystem Library & C++17
Both boost filesystem library & C++17 Filesystem provides 2 member function for path class under different namespaces i.e. std::experimental::filesystem for C++17 & boost::filesystem for Boost Filesystem Library.
path path::extension() const;
It returns the path object pointing to the extension component of given path object. If file has no extension then it will return empty path object.
bool path::has_extension() const;
Returns true if given file in path object has extension.
Use these to get the extension of a given file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* * Get File extension from File path or File Name */ std::string getFileExtension(std::string filePath) { // Create a Path object from given string filesys::path pathObj(filePath); // Check if file name in the path object has extension if (pathObj.has_extension()) { // Fetch the extension from path object and return return pathObj.extension().string(); } // In case of no extension return empty string return ""; } |
It will return empty string if given file has no extension.
In case of c++17 use this header file and namespace,
1 2 3 |
#include <experimental/filesystem> namespace filesys = std::experimental::filesystem; |
In case of Boost Library use this header file and namespace,
1 2 3 |
#include <boost/filesystem.hpp> namespace filesys = boost::filesystem; |
Complete boost example,
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 |
#include <iostream> #include <string> #include <cassert> #include <boost/filesystem.hpp> namespace filesys = boost::filesystem; /* * Get File extension from File path or File Name */ std::string getFileExtension(std::string filePath) { // Create a Path object from given string filesys::path pathObj(filePath); // Check if file name in the path object has extension if (pathObj.has_extension()) { // Fetch the extension from path object and return return pathObj.extension().string(); } // In case of no extension return empty string return ""; } int main() { std::string filePath = "/home/user/sample/temp/logs.out"; // Get the extension std::string extension = getFileExtension(filePath); std::cout<<"Ext : "<<extension<<std::endl; // extension should be equal to ".out" assert(extension == ".out"); filePath = "/home/user/sample/temp/"; extension = getFileExtension(filePath); std::cout<<"Ext : "<<extension<<std::endl; // extension should be equal to "" assert(extension == ""); return 0; } |
Output:
1 2 |
Ext : .out Ext : |
To compile the above example using Boost, use following command,
1 |
g++ -std=c++11 example.cpp -lboost_filesystem -lboost_system |
To compile the above example using C++17, use following command,
1 |
g++ --std=c++17 extension_3.cpp -lstdc++fs |
Fetch extension of a given file using std::string functions
Steps to search for an extension in given path string,
- Search for the last occurrence of ‘.’
- If exists then return the substring from occurrence of ‘.’ to last chaarcter.
- Else return empty string.
Complete example 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 |
#include <iostream> #include <cassert> #include <string> /* * Get File extension from File path or File Name */ std::string getFileExtension(std::string filePath) { // Find the last position of '.' in given string std::size_t pos = filePath.rfind('.'); // If last '.' is found if (pos != std::string::npos) { // return the substring return filePath.substr(pos); } // In case of no extension return empty string return ""; } int main() { std::string filePath = "/home/user/sample/temp/logs.out"; // Get the extension std::string extension = getFileExtension(filePath); std::cout<<"Ext : "<<extension<<std::endl; // extension should be equal to ".out" assert(extension == ".out"); filePath = "/home/user/sample/temp/"; extension = getFileExtension(filePath); std::cout<<"Ext : "<<extension<<std::endl; // extension should be equal to "" assert(extension == ""); return 0; } |
Output:
1 2 |
Ext : .out Ext : |
Follow @thisPtr
Leave a Reply