How to Get File Extension in C++? Last Updated : 16 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, we may often find the need to extract the file extension from a given path of the file while working in many applications for processing or validating. In this article, we will learn how to get the file extension in C++. For Example, Input: someFolder ↳ filename.ext Output: File Extension = .ext Extracting a File Extension in C++In C++17 and later, the std::filesystem::path::extension() function from the std::filesystem library can be used to retrieve the extension of a file from its path which is like a string wrapper. This function specifically extracts and returns the file extension that can be later used for processing. It is the member function of the std::filesystem::path class so we can call it using the dot operator (.). C++ Program to Get File ExtensionThe below program demonstrates how we can get a file extension from a file path in C++. C++ // C++ Program to demonstrate how we can get a file // extension from a file path #include <filesystem> #include <iostream> using namespace std; int main() { // Define a path object representing the file path. filesystem::path filePath = "C:/Users/Desktop/myFile.txt"; // Print the file extension using the extension() method // of the path object. cout << "File extension is: " << filePath.extension() << endl; return 0; } Output File extension is: ".txt"Explanation: In the above example, we first define the file path and then use the extension() function to get the file extension. Finally, the extension is printed. Note: The extension() function works only in C++17 and later and it includes the dot in the extension. If the file does not have an extension, it will return an empty string. Comment More infoAdvertise with us Next Article How to Get File Extension in C++? A anushamahajan5 Follow Improve Article Tags : C++ Programs C++ cpp-file-handling C++ File Programs CPP Examples +1 More Practice Tags : CPP Similar Reads How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the 2 min read How Can I Get a File Size in C++? In C++, we often need to determine the size of a file which is the number of bytes in a file. This is done for various applications, such as file processing or validation. In this article, we will learn how to get the file size in C++. Example: Input: FilePath = "C:/Users/Desktop/myFile.txt" Output: 2 min read How to Get Current Directory in C++ The current working directory, also known as the present working directory, is the location on the file system where the executing program is located and operates from. When working with files and directories in C++, it is important to determine the current working directory to find the resource fil 2 min read How to Get Environment Variable in C++? Environment variables are globally accessible named values that store information about the system environment where your code is executed. They are also used to store configuration settings, paths to important directories, and other system-specific data as well. In this article, we will learn how t 2 min read How to Extract File Name and Extension From a Path in C++? Prerequisites: Install C++17 or Newer Standard Compiler It is a common requirement of users to extract filenames and their extensions from a given path. The process is trivial, but it becomes hard under some circumstances (different OS, file types, etc.). This article will teach you how to extract t 2 min read Like