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.

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

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

pathObj.filename().string();

Checkout complete function as follows,
/*
 * 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,
#include <boost/filesystem.hpp>

namespace filesys = boost::filesystem;

To use the above function with C++17 FileSystem Library, use following header file and namesapce,
#include <experimental/filesystem>

namespace filesys = std::experimental::filesystem;

Complete executable example using Boost is as follows,
#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:
data.csv
data

To compile the above code in linux using Boost Filesystem Library, use following command,
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,
g++ --std=c++17 example.cpp -lstdc++fs

Get File Name using C++ std::string functions

#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:
data.csv
data.csv
data.csv

 

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