How to Extract File Name and Extension From a Path in C++? Last Updated : 27 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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 the file name and extension from a path in C++. Extracting file names and extensions from a Path Firstly the path to the file is defined in the variable file path. This variable is sent as an argument to the filesystem::path class constructor. Then we use the public member function filename to get the filename and extension from the path. Then used, the stem member function to get the file name, and in the end, used the extension member function to get the file extension. Code: C++ // C++ Program for Extracting // file names and extensions from a path #include <filesystem> #include <iostream> using namespace std; int main() { // The path to the file char* filepath = "C:\\Users\\Drunk\\Desktop\\l.png"; // Passing the path as argument to the function filesystem::path p(filepath); // Displaying filename and extensions separately // By accessing the respective constructors cout << "filename and extension: " << p.filename() << std::endl; cout << "filename only: " << p.stem() << std::endl; cout << "Extension: " << p.extension(); return 0; } Output: Output Note: The above method would also work on extension-less files. They would contain a file name but would have an empty string as the extension. Ex. If the file's path is changed to C:\Users\Apples\touch Output: Output Comment More infoAdvertise with us Next Article How to Extract File Name and Extension From a Path in C++? V vasudev4 Follow Improve Article Tags : Technical Scripter C++ Programs C++ Technical Scripter 2022 Practice Tags : CPP Similar Reads How to Get File Extension in C++? 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 = 2 min read How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read How to Extract a Substring from a Character Array in C++? In C++, character arrays are used to store sequences of characters also known as strings. A substring is a part of a string that consists of a continuous sequence of characters from the string. In this article, we will learn how to extract a substring from a character array in C++. Extract a Substri 2 min read How to Read a File Character by Character in C++? In C++, file handling is used to store data permanently in a computer. Using file handling we can store our data in secondary memory (Hard disk). In this article, we will learn how to read a file character by character in C++. Example: Input: "Geeks for Geeks"Output: G e e k s f o r G e e k sRead Te 2 min read How to Check a File or Directory Exists in C++? Checking the presence of a directory or a file is one of the most common operations performed by a file system in an Operating System. Most programming languages offer some level of file system accessibility in form of library functions. In this article, you will learn how to test a file or director 4 min read Like