• c++标准库学习-filesystem文件系统库(1)-path对象


    前言

    想赶紧把c++用起来,先熟悉一下c++的标准库

    一.path对象

    path对象是filesystem中表示路径的数据结构,path可隐式转换自及转换成 std::basic_string ,这使得在文件 API 上使用它们可行,例如作为到 std::ifstream::open 的参数。path中_M_pathname成员保存的就是我们的路径字符串,但是_M_pathname是一个私有成员。

    1.path的构造

    首先从代表路径的字符串构造:

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="~/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    然后也可以从其他path对象复制构造:

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="~/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
    	fs::path p2=p1;
    	std::cout<<"p2:"<<p2<<std::endl;
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出结果:

    p1:"~/桌面/dter/detr/test_all.py"
    p2:"~/桌面/dter/detr/test_all.py"
    
    • 1
    • 2

    二.path对象的常用成员方法

    1.append和operator/=

    append和operator/=用于路径的连接上,连接路径的时候append和operator/=默认会自动加上/分割符号。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="~/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
    	fs::path p2=p1;
    	std::cout<<"p2:"<<p2<<std::endl;
        p2.clear();
        p2=p2.assign("~/桌面/dter/detr");
        std::cout<<"p2:"<<p2<<std::endl;
        p2=p2/"test_all.py";
        std::cout<<"p2:"<<p2<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    输出结果:

    p1:"~/桌面/dter/detr/test_all.py"
    p2:"~/桌面/dter/detr/test_all.py"
    p2:"~/桌面/dter/detr"
    p2:"~/桌面/dter/detr/test_all.py"
    
    • 1
    • 2
    • 3
    • 4

    2.concat和operator+=

    concat和operator+= 同样是来做路径连接的,但是和append和operator/=,concat和operator+= 不会自动添加分割符/。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="~/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
    	fs::path p2=p1;
    	std::cout<<"p2:"<<p2<<std::endl;
        p2.clear();
        p2=p2.assign("~/桌面/dter/detr");
        std::cout<<"p2:"<<p2<<std::endl;
        p2=p2+="/test_all.py";
        std::cout<<"p2:"<<p2<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    输出结果和上面一致

    3.remove_filename

    remove_filename方法可以移除路径上的文件,注意这个方法不会移除目录。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="~/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
        p1=p1.remove_filename();
        std::cout<<"p1:"<<p1<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    输出结果:

    p1:"~/桌面/dter/detr/test_all.py"
    p1:"~/桌面/dter/detr/"
    
    • 1
    • 2

    4.replace_extension

    replace_extension方法会替代路径中文件的扩展名。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="~/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
        p1=p1.replace_extension("cpp");
        std::cout<<"p1:"<<p1<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    输出结果:

    p1:"~/桌面/dter/detr/test_all.py"
    p1:"~/桌面/dter/detr/test_all.cpp"
    
    • 1
    • 2

    5.swap

    swap方法会交换两个path的路径。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="~/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
        fs::path p2="./path.cpp";
        std::cout<<"p2:"<<p2<<std::endl;
        p1.swap(p2);
        std::cout<<"p1:"<<p1<<std::endl;
        std::cout<<"p2:"<<p2<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    6.c_str, native

    native以引用的方式返回path中的路径字符串,而c_str就相当于native().c_str()。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="~/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
        std::string native=p1.native();
        std::cout<<native<<std::endl;
        const char *c_str=p1.c_str();
        std::cout<<c_str<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    7.root_name,root_directory,root_path

    root_name返回通用格式路径的根名,root_directory返回通用格式路径的根目录,root_path返回路径的根路径。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="/home/ztyf/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
        std::cout<<p1.root_directory()<<std::endl;
        std::cout<<p1.root_path()<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    输出结果:

    p1:"/home/ztyf/桌面/dter/detr/test_all.py"
    "/"
    "/"
    
    • 1
    • 2
    • 3

    这里注意的是需要输入绝对路径才行。

    8.relative_path,parent_path

    relative_path返回拆除root的路径,parent_path返回到上一级目录

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="/home/ztyf/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
        std::cout<<p1.relative_path()<<std::endl;
        std::cout<<p1.parent_path()<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    输出结果:

    p1:"/home/ztyf/桌面/dter/detr/test_all.py"
    "home/ztyf/桌面/dter/detr/test_all.py"
    "/home/ztyf/桌面/dter/detr"
    
    • 1
    • 2
    • 3

    9.filename,stem,extension

    filename返回路径中的文件名,stem返回路径中的文件名,但是不带扩展名,extension返回路径中文件的扩展名。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="/home/ztyf/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
        std::cout<<p1.filename()<<std::endl;
        std::cout<<p1.stem()<<std::endl;
        std::cout<<p1.extension()<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出结果:

    p1:"/home/ztyf/桌面/dter/detr/test_all.py"
    "test_all.py"
    "test_all"
    ".py"
    
    • 1
    • 2
    • 3
    • 4

    10.empty

    检测path对象是否为空。

    #include
    #include
    namespace fs = std::filesystem;
    int main(int argc,char *argv[])
    {
    	fs::path p1="/home/ztyf/桌面/dter/detr/test_all.py";
    	std::cout<<"p1:"<<p1<<std::endl;
        fs::path p2="./path.cpp";
        p2.clear();
        std::cout<<"p1 empty?"<<p1.empty()<<"   "<<"p2 empty?"<<p2.empty()<<std::endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出结果:

    p1:"/home/ztyf/桌面/dter/detr/test_all.py"
    p1 empty?0   p2 empty?1
    
    • 1
    • 2

    三.总结

    暂时就写这么多吧。

  • 相关阅读:
    安卓案例:学生信息管理
    Linux网络编程- ether_header & iphdr & tcphdr
    基于Java的旅游网站系统设计与实现(源码+lw+部署文档+讲解等)
    [附源码]JAVA毕业设计高校心理咨询预约系统(系统+LW)
    如何高效安装MindSpore的GPU版本
    SSM+线上诊疗系统 毕业设计-附源码161711
    SpringBoot缓存
    【JavaScript脚本宇宙】打造完美用户体验:六大模态库全解析
    1704. 判断字符串的两半是否相似
    RTLinux的介绍
  • 原文地址:https://blog.csdn.net/qq_52095705/article/details/126128325