C++17 引入 <filesystem> 標準庫,提供跨平台、類型安全的路徑操作,是**首選方案**。
#include <iostream>
#include <filesystem>
#include <string>
namespace fs = std::filesystem;
int main() {
std::string path = "c:\\new\\music.mp3"; // Windows 路徑需轉義或用原始字符串
// std::string path = R"(c:\new\music.mp3)"; // C++11 原始字符串字面量,無需轉義
fs::path p(path);
std::string filename = p.filename().string(); // 獲取文件名 "music.mp3"
std::cout << "文件名: " << filename << std::endl;
// 輸出: 文件名: music.mp3
return 0;
}
parent_path(), extension(), stem() (不帶擴展名文件名) 等。若無法使用 C++17,可手動查找最後一個路徑分隔符:
#include <iostream>
#include <string>
std::string extractFilename(const std::string& path) {
if (path.empty()) return "";
// 查找最後一個 '/' 或 '\'
size_t pos = path.find_last_of("/\\");
// npos 表示未找到 (如 "music.mp3" 純文件名),返回整個字符串
if (pos == std::string::npos) return path;
// 返回分隔符後的子串
return path.substr(pos + 1);
}
int main() {
std::string paths[] = {
"c:\\new\\music.mp3",
"/home/user/document.pdf",
"relative/path/image.png",
"no_slash.txt",
"trailing_slash/",
""
};
for (const auto& p : paths) {
std::cout << "路徑: [" << p << "] -> 文件名: [" << extractFilename(p) << "]" << std::endl;
}
return 0;
}
路徑: [c:\new\music.mp3] -> 文件名: [music.mp3] 路徑: [/home/user/document.pdf] -> 文件名: [document.pdf] 路徑: [relative/path/image.png] -> 文件名: [image.png] 路徑: [no_slash.txt] -> 文件名: [no_slash.txt] 路徑: [trailing_slash/] -> 文件名: [] (目錄無文件名) 路徑: [] -> 文件名: []
#include <boost/filesystem.hpp>
#include <iostream>
#include <string>
namespace bfs = boost::filesystem;
int main() {
std::string path = "c:\\new\\music.mp3";
bfs::path p(path);
std::cout << p.filename().string() << std::endl; // music.mp3
return 0;
}
std::filesystem::path::filename(),最安全、可移植、可維護。extractFilename 函數處理雙分隔符兼容,注意邊界情況 (目錄路徑、空串)。