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,

/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.

/*
 * 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,

#include <experimental/filesystem>

namespace filesys = std::experimental::filesystem;

In case of Boost Library use this header file and namespace,
#include <boost/filesystem.hpp>

namespace filesys = boost::filesystem;

Complete boost example,
#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:
Ext : .out
Ext :

To compile the above example using Boost, use following command,
g++ -std=c++11 example.cpp -lboost_filesystem -lboost_system

To compile the above example using C++17, use following command,
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,

#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:
Ext : .out
Ext :

 

Join a list of 2000+ Programmers for latest Tips & Tutorials