在C ++中如何验证文件或文件夹路径? [英] In C++ how do i validate a file or folder path?

查看:246
本文介绍了在C ++中如何验证文件或文件夹路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标路径的用户输入字符串可能包含空格或其他无效字符。

A user input string for a destination path can potentially contain spaces or other invalid characters.

示例:C:\users\username\\ directoryname \

Example: " C:\users\username\ \directoryname\ "

请注意,在路径的两侧都有空格,以及中间只有一个空格的无效文件夹名称。检查它是否是一个绝对路径是不够的,因为只有真正处理前导空白。删除尾部空白字符也不够,因为您在中间仍然留有无效的space-for-folder-name。

Note that this has whitespace on both sides of the path as well as an invalid folder name of just a space in the middle. Checking to see if it is an absolute path is insufficient because that only really handles the leading whitespace. Removing trailing whitespace is also insufficient because you're still left with the invalid space-for-folder-name in the middle.

如何证明路径有效

推荐答案

证明路径的唯一方法是有效的是打开它。

The only way to "prove" the path is valid is to open it.

SHLWAPI 提供了一组路径函数,可用于规范化路径或验证路径是否有效。这可以用来拒绝明显不良的路径,但是你仍然不能相信路径是有效的,无需通过文件系统。

SHLWAPI provides a set of path functions which can be used to canonicalize the path or verify that a path seems to be valid. This can be useful to reject obviously bad paths but you still cannot trust that the path is valid without going through the file system.

对于NTFS,我相信你给的路径实际上是有效的(尽管Explorer可能不允许创建只有空格的目录。)

With NTFS, I believe the path you give is actually valid (though Explorer may not allow you to create a directory with only a space.)

这篇关于在C ++中如何验证文件或文件夹路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆