C++如何在不使用 winapi 的情况下移动文件并将其从一个磁盘复制到另一个磁盘?

发布于 2024-12-29 23:09:16 字数 137 浏览 2 评论 0原文

它必须是纯c++,我知道system("copy c:\\test.txt d:\\test.txt");但我认为这是系统函数,而不是c++解决方案,否则我会出错吗?

It must be pure c++, I know about system("copy c:\\test.txt d:\\test.txt"); but I think it's the system function, not c++ solution, or I can mistakes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛溺水了 2025-01-05 23:09:16

std::fstream 怎么样?打开一个用于读取,另一个用于写入,并使用 std::copy 让标准库处理复制。

像这样的事情:

void copy_file(const std::string &from, const std::string &to)
{
    std::ifstream is(from, ios::in | ios::binary);
    std::ofstream os(to, ios::out | ios::binary);

    std::copy(std::istream_iterator<char>(is), std::istream_iterator<char>(),
              std::ostream_iterator<char>(os));
}

How about std::fstream? Open one for reading and another for writing, and use std::copy to let the standard library handle the copying.

Something like this:

void copy_file(const std::string &from, const std::string &to)
{
    std::ifstream is(from, ios::in | ios::binary);
    std::ofstream os(to, ios::out | ios::binary);

    std::copy(std::istream_iterator<char>(is), std::istream_iterator<char>(),
              std::ostream_iterator<char>(os));
}
浅笑依然 2025-01-05 23:09:16

尝试使用 boost 中的 copy_file

#include <boost/filesystem.hpp>

boost::filesystem::copy_file("c:\\test.txt","d:\\test.txt");

如果有错误,它会抛出异常。有关更多文档,请参阅此页面:http: //www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#copy_file

Try using copy_file from boost.

#include <boost/filesystem.hpp>

boost::filesystem::copy_file("c:\\test.txt","d:\\test.txt");

It will throw an exception if there is an error. See this page for more documentation: http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#copy_file

夏有森光若流苏 2025-01-05 23:09:16

我喜欢使用标准 STL 运算符的简单流方法:

std::ifstream ifs("somefile", std::ios::in | std::ios::binary);
std::ofstream ofs("newfile", std::ios::out | std::ios::binary);
ofs << ifs.rdbuf();

这里的想法是有一个 operator<< (streambuf*) 用于 std::ofstream,因此您只需将与输入流关联的 streambuf 传递给它即可。

为了完整起见,您可以执行如下操作:

bool exists(const std::string& s) {
    std::ifstream istr(s, std::ios::in | std::ios::binary);
    return istr.is_open();
}

void copyfile(const std::string& from, const std::string& to) {
    if (!exists(to)) {
        std::ifstream ifs(from, std::ios::in | std::ios::binary);
        std::ofstream ofs(to, std::ios::out | std::ios::binary);
        ofs << ifs.rdbuf();
    }
}

如果目标尚不存在,则只会复制文件。只是对完整性进行额外检查:)

关于移动文件,在“标准”C++中,我可能会复制该文件(如上所述),然后删除它,执行以下操作:

if (0 != remove(from.c_str())) {
    // remove failed
}

除了使用诸如 boost 我不相信还有另一种标准的、可移植的方法来删除文件。

I like the simple streaming approach, using standard STL operators:

std::ifstream ifs("somefile", std::ios::in | std::ios::binary);
std::ofstream ofs("newfile", std::ios::out | std::ios::binary);
ofs << ifs.rdbuf();

The idea here is that there is an operator<< (streambuf*) for std::ofstream, so you simply pass it the streambuf associated with your input stream.

For completeness, you could do something like the following:

bool exists(const std::string& s) {
    std::ifstream istr(s, std::ios::in | std::ios::binary);
    return istr.is_open();
}

void copyfile(const std::string& from, const std::string& to) {
    if (!exists(to)) {
        std::ifstream ifs(from, std::ios::in | std::ios::binary);
        std::ofstream ofs(to, std::ios::out | std::ios::binary);
        ofs << ifs.rdbuf();
    }
}

This would only copy the file if the destination didn't already exist. Just an extra check for sanity :)

Regarding moving a file, in "standard" C++ I would probably copy the file (as above), and then delete it, doing something like:

if (0 != remove(from.c_str())) {
    // remove failed
}

Aside from using something like boost I'm not convinced there's another standard, portable way to delete a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文