以下是使用C++98标准实现获取除文件名的所有路径的代码:

#include <iostream>
#include <string>

std::string getDirectoryPath(const std::string& filePath) {
    std::string directoryPath = filePath;
    
    // Remove the file name from the end
    size_t lastSlashIndex = directoryPath.find_last_of('/');
    if (lastSlashIndex != std::string::npos) {
        directoryPath = directoryPath.substr(0, lastSlashIndex);
    }
    
    // Remove the file name from the beginning (in case of Windows path)
    size_t lastBackslashIndex = directoryPath.find_last_of('\\');
    if (lastBackslashIndex != std::string::npos) {
        directoryPath = directoryPath.substr(0, lastBackslashIndex);
    }
    
    return directoryPath;
}

int main() {
    std::string filePath = "hdfs://192.168.100.1:9000/opt/file_video_orig/C/libRcfDll.so";
    std::string directoryPath = getDirectoryPath(filePath);
    std::cout << "Directory Path: " << directoryPath << std::endl;
    
    return 0;
}

输出结果:

Directory Path: hdfs://192.168.100.1:9000/opt/file_video_orig/C
``
linux cc++ 获取除文件名的所有路径 hdfs19216810019000optfile_video_origClibRcfDllso使用c++98标准给出实现代码

原文地址: https://www.cveoy.top/t/topic/iwzG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录