C++ || How To Get The File Path, File Name & File Extension From A Path Using C++
The following is a module with functions which demonstrates how to parse a file path, file name and file extension from a path using C++.
1. Get File Path
The example below demonstrates the use of ‘Utils::getFilePath‘ to parse and get the file path from a path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Get File Path // Declare path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the file path auto filePath = Utils::getFilePath(path); std::cout << "Path: " << path << std::endl; std::cout << "File Path: " << filePath; // expected output: /* Path: C:\Users\Name\Desktop\20171216_155433.jpg File Path: C:\Users\Name\Desktop */ |
2. 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 15 16 |
// Get File Name // Declare path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the file name auto fileName = Utils::getFileName(path); std::cout << "Path: " << path << std::endl; std::cout << "File Name: " << fileName; // expected output: /* Path: C:\Users\Name\Desktop\20171216_155433.jpg File Name: 20171216_155433.jpg */ |
3. 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 15 16 |
// 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 << std::endl; std::cout << "File Extension: " << extension; // expected output: /* Path: C:\Users\Name\Desktop\20171216_155433.jpg File Extension: jpg */ |
4. 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// ============================================================================ // 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: getFilePath * USE: Returns the path from a given file path * @param path: The path of the file * @return: The path from the given file path */ std::string getFilePath(const std::string& path) { auto pathEnd = path.find_last_of("/\\"); auto pathName = pathEnd == std::string::npos ? path : path.substr(0, pathEnd); return pathName; } /** * FUNCTION: getFileName * USE: Returns the file name from a given file path * @param path: The path of the file * @return: The file name from the given file 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 file path * @param path: The path of the file * @return: The file extension from the given file 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 static_cast<unsigned char>(std::tolower(c)); }); return ext; } }// http://programmingnotes.org/ |
5. 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 41 42 43 44 45 |
// ============================================================================ // 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 file path auto filePath = Utils::getFilePath(path); display("Path: " + path + ", File Path: " + filePath); // 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