filesystem::copy 返回无效参数,但参数是现有的 fs::path 变量

问题描述 投票:0回答:1

我正在使用 std::filesystem (fs) 库处理项目中的文件。我存储为 fs::path 变量的所有路径。我的代码是:

int main(){

    std::filesystem::path root_dir = "/home/xyz/study/misis2023f-22-3-kovaleva-m-a/prj.cw/test_dir";

    std::filesystem::path from_path = "/home/xyz/study/misis2023f-22-3-kovaleva-m-a/prj.cw/test_dir/misis2023_kovaleva_m_a/make.cpp";
    std::cout << std::filesystem::exists(from_path) << '\n';

    std::filesystem::copy_file(from_path, root_dir, std::filesystem::copy_options::update_existing);
}

当我调用 fs::copy 函数时,我遇到问题,参数无效。但正如您所见,其他函数(例如 fs::exists)可以与这些变量一起正常工作:

抛出 'std::filesystem::__cxx11::filesystem_error' 实例后调用终止 什么():文件系统错误:无法复制文件:无效参数[/home/xyz/study/misis2023f-22-3-kovaleva-m-a/prj.cw/test_dir/misis2023_kovaleva_m_a/make.cpp] [/home/xyz/study /misis2023f-22-3-kovaleva-m-a/prj.cw/test_dir]

函数签名:(来自 cpp-reference)

void copy( const std::filesystem::path& from,
           const std::filesystem::path& to, 
           std::filesystem::copy_options options );

如何通过争论解决这个问题?

我使用cmake 3.22.1,ubuntu 22.04

我尝试将参数作为字符串传递:

std::filesystem::copy_file("/home/xyz/study/misis2023f-22-3-kovaleva-m-a/prj.cw/test_dir/misis2023_kovaleva_m_a/make.cpp", root_dir, std::filesystem::copy_options::update_existing);

我试图通过更简短的论点:

std::filesystem::path from_path = "/home/xyz/study/test_copy.txt";
std::filesystem::copy_file(from_path, root_dir, std::filesystem::copy_options::update_existing);

我也尝试过不带扩展名地传递参数:

std::filesystem::path from_path = "/home/xyz/study/what";
std::filesystem::copy_options::update_existing);

但结果是一样的:无效参数

我还尝试检查文件状态

std::filesystem::file_status from_path_status = std::filesystem::status(from_path);

输出是:

_M_type: std::filesystem:;file_type::regular
_M_perms: 436
c++ std file-copying std-filesystem
1个回答
0
投票

您正在将 directory 路径传递给

std::filesystem::copy_file()
,它需要两个 file 路径。

请使用

std::filesystem::copy()
来代替。

© www.soinside.com 2019 - 2024. All rights reserved.