【发布时间】:2020-11-12 19:48:03
【问题描述】:
假设我定义了以下函数,用于根据提供给它的路径检查所需文件是否可用。
bool check_my_file_exists( const std::wstring& my_root_file )
{
const std::wstring file_path = L"..\\..\\require_file.txt";
const std::wstring relative_path_to_required_file = my_root_file + L"\\" + file_path;
if ( !boost::filesystem::exists( relative_path_to_required_file ))
{
return false;
}
return true;
}
假设 D:\my_file\require_file.txt 存在并且, 当使用文件的绝对路径作为参数调用此函数时,它总是失败
check_my_file_exists( L"D:\\my_files\\this_folder\\that_folder\\root.file" ); // return false
但是当指定根文件的父文件夹的绝对路径作为参数时,这会按预期工作,
check_my_file_exists( L"D:\\my_files\\this_folder\\that_folder" ); // return true
在资源管理器中使用 D:\my_files\this_folder\that_folder\root.file....\required_file.txt 也可以打开 required_file.txt。
环境:
- 提升:1.69
- Visual Studio 2017
- Windows 10、2004
我很困惑这是一些提升实施问题还是预期的行为。
【问题讨论】:
-
检查您的工作目录并访问
-
您不能将
..附加到普通文件名,只能附加到目录。 -
@n.'pronouns'm。是的,这是有道理的。但是如果在 Windows 资源管理器中尝试,这种情况是有效的。
-
是的,您可以在 Windows 资源管理器中执行此操作。现在呢?
-
正如你所说,相对路径应该来自工作目录,而不是文件。
标签: c++ visual-c++ boost