gpt4 book ai didi

c++ - 当指定文件的相对路径时,boost::filesystem::exists()失败

转载 作者:行者123 更新时间:2023-12-01 14:47:18 25 4
gpt4 key购买 nike

假设我定义了以下函数,并根据提供给它的路径来检查所需文件是否可用。

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年

  • 我对这是一些实现问题还是预期的行为感到困惑。

    最佳答案

    此处的boost::filesystem::exists()函数没有问题。
    根据您的示例require_file.txt中存在"D:\\my_files\\this_folder\\that_folder\\..\\..\\require_file.txt",其中就绝对路径而言,它是"D:\\my_files\\require_file.txt",请牢记此实际位置以作进一步说明。

    if ( !boost::filesystem::exists( "D:\\my_files\\this_folder\\that_folder\\root.file\\..\\..\\require_file.txt"))
    {
    return false;
    }
    因此,相对于绝对路径,构造的相对路径是 "D:\\my_files\\this_folder\\that_folder\\root.file\\..\\..\\require_file.txt"。因此,您正在检查错误的路径,这就是 "D:\\my_files\\this_folder\\require_file.txt"函数始终返回false的原因。

    关于c++ - 当指定文件的相对路径时,boost::filesystem::exists()失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63048250/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com