C++ || How To Get The File Name & File Extension From A Path Using C++

The following is a module with functions which demonstrates how to parse a file name and file extension from a path using C++.
1. Get File Name
The example below demonstrates the use of ‘Utils::getFileName‘ to parse and get the file name from a path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Get File Name // Declare path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the filename auto fileName = Utils::getFileName(path); std::cout << "Path: " + path + ", File Name: " + fileName; // expected output: /* Path: C:\Users\Name\Desktop\20171216_155433.jpg, File Name: 20171216_155433.jpg */ |
2. Get File Name Extension
The example below demonstrates the use of ‘Utils::getFileExtension‘ to parse and get the file extension from a path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Get File Name Extension // Declare path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the file extension auto extension = Utils::getFileExtension(path); std::cout << "Path: " + path + ", File Extension: " + extension; // expected output: /* Path: C:\Users\Name\Desktop\20171216_155433.jpg, File Extension: jpg */ |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 |
// ============================================================================ // Author: Kenneth Perkins // Date: Dec 5, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.h // Description: Handles general utility functions // ============================================================================ #pragma once #include <string> #include <algorithm> #include <cctype> namespace Utils { /** * FUNCTION: getFileName * USE: Returns the file name from a given path * @param path: The path of the file * @return: The file name from the given path */ std::string getFileName(const std::string& path) { auto fileNameStart = path.find_last_of("/\\"); auto fileName = fileNameStart == std::string::npos ? path : path.substr(fileNameStart + 1); return fileName; } /** * FUNCTION: getFileExtension * USE: Returns the file extension from a given path * @param path: The path of the file * @return: The file extension from the given path */ std::string getFileExtension(const std::string& path) { auto fileName = getFileName(path); auto extStart = fileName.find_last_of('.'); auto ext = extStart == std::string::npos ? "" : fileName.substr(extStart + 1); std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return std::tolower(c); }); return ext; } }// http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 |
// ============================================================================ // Author: Kenneth Perkins // Date: Dec 5, 2020 // Taken From: http://programmingnotes.org/ // File: program.cpp // Description: The following demonstrates the use of the Utils Namespace. // ============================================================================ #include <iostream> #include <string> #include <exception> #include "Utils.h" void display(const std::string& message); int main() { try { // Declare path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the filename auto fileName = Utils::getFileName(path); display("Path: " + path + ", File Name: " + fileName); // Get the file extension auto extension = Utils::getFileExtension(path); display("Path: " + path + ", File Extension: " + extension); } catch (std::exception& e) { display("\nAn error occurred: " + std::string(e.what())); } std::cin.get(); return 0; } void display(const std::string& message) { std::cout << message << std::endl; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply