QDir类cleanPath函数用法

本文介绍了一种处理文件路径的算法,通过规范化目录分隔符、移除冗余符号及解析.和..来简化路径。例如,./local变为local,local/../bin变为bin,/local/usr/../bin变为/local/bin。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过规则处理让源路径成为最短等价路径

处理规则如下

1.使用单斜线取代多斜线
2.取消每个包含.名称的路径
3.取消内部包含..的路径
4.取消/..根路径的元素,使用/替换

这个过程是循环执行的,直到路径符合所有规则

示例

QStringList ls;
	ls << "a/c" << "a//c" << "a/c/." << "a/c/b/.." << "/../a/c" << "/../a/b/../././/c" << "";
	for (auto& t : ls)
	{
	  QString k =	QDir::cleanPath(t);
	  qDebug() << k << "\r\n";
	}

输出如下:


 

 而/../a/c其实就是/a/c, Qt官方帮助手册中提到如下:

Returns path with directory separators normalized (that is, platform-native separators converted to "/") and redundant ones removed, and "."s and ".."s resolved (as far as possible).

Symbolic links are kept. This function does not return the canonical path, but rather the simplest version of the input. For example, "./local" becomes "local", "local/../bin" becomes "bin" and "/local/usr/../bin" becomes "/local/bin".

红色的表示:在不影响正确的情况,在返回的路径中,尽量保留  . ..

这也就是输出/../a/c而不是/a/c的原因。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值