C++学习日志41------path类:获取磁盘空间、路径分解、特殊运算符


一、获取磁盘空间

#include<iostream>
#include<filesystem>

int main()
{
	using std::cout;
	using std::endl;
	namespace fs = std::filesystem;

	//定义路径对象
	fs::path p{ "C:\\" };
	//展示磁盘的总大小和剩余大小
	cout << "C:total space:" << fs::space(p).capacity << endl;
	cout << "C:free space:" << fs::space(p).free << endl;

	std::cin.get();
	return 0;
}

在这里插入图片描述
结果如上图所示。

二、路径分解

#include<iostream>
#include<filesystem>
using std::cout;
using std::endl;
int main()
{
	namespace fs = std::filesystem;
	//定义路径p
	fs::path p{ R"(E:\C_pp\hello.txt)" };
	//是否存在?根名?根路径?相对路径?
	if (p.empty())
	{
		cout << "Path" << p << "is empty." << endl;
	}
	if (!fs::exists(p))
	{
		cout << "Path" << p << "does not exist." << endl;
		std::exit(0);
	}

	cout << "root_name(): " << p.root_name() << "\n"
		<< "root_path():" << p.root_path() << "\n"
		<< "relative_path():" << p.relative_path() << "\n";
	//父路径?文件名?文件名主干?拓展名?
	cout << "parent_path():" << p.parent_path() << "\n"
		<< "filename():" << p.filename() << "\n"
		<< "stem():" << p.stem() << "\n"
		<< "extension():" << p.extension() << endl;

	std::cin.get();
}

在这里插入图片描述
结果如上图所示。

三、特殊运算符

#include<iostream>
#include<filesystem>
using std::cout;
using std::endl;
namespace fs = std::filesystem;
int main()
{
	fs::path p1{ R"(E:\C_pp)" };
	fs::path p2{ R"(E:\C_pp)" };
	fs::path p3{ " " };

	//append和/=  会自动添加/
	p1.append(R"(users)");
	p1 /= R"(cyd)";
	cout << p1 << endl;

	//concat和+=    不会自动添加/ 因此输出结果不合法
	p2.concat(R"(users)");
	p2 += R"(cyd)";

	cout << p2 << endl;

	//用运算符/拼凑一个新路径
	p3 = p3 / R"(C:temp)" / R"(users)" / R"(cyd)";

	cout << p3 << endl;
	std::cin.get();
}

在这里插入图片描述
结果如上图所示。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@白圭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值