在Linux上的C ++中移动文件的更快方法 [英] Faster way to move file in c++ on linux

查看:513
本文介绍了在Linux上的C ++中移动文件的更快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C ++在Linux上移动文件. 问题在于,源文件和目标文件夹可以位于不同的分区中.所以我不能简单地移动文件. 好的.我决定复制文件并删除旧文件.

I'm trying to move files on linux by using C++. The Problem is, that the source file and the destination folder can be in different partitions. So I can't simply move the files. Ok. I decided to copy the file and delete the old one.

//-----
bool copyFile(string source, string destination)
{
    bool retval = false;
    ifstream srcF (source.c_str(), fstream::binary);
    ofstream destF (destination.c_str(), fstream::trunc|fstream::binary);
    if(srcF.is_open() && destF.is_open()){
        destF << srcF.rdbuf(); //copy files binary stream
        retval = true;
    }
    srcF.close();
    destF.close();
    return retval;
}
//-----

现在是我的问题. 我意识到,这种方法非常慢. 100MB需要47秒. 只需使用console命令复制文件,就需要2-3秒.

Now my problem. I realized, this method is very slow. It takes 47 seconds for 100MB. Simply copy a file with the console command takes 2-3 seconds.

有人有主意吗?

推荐答案

众所周知,流非常慢.您可以使用操作系统提供的工具,也可以使用某些便携式包装器.

Streams are known to be pretty slow. You can either use tools provided by operating system or you can use some portable wrapper.

我建议使用boost::filesystem,因为计划将其添加到STL(C ++ 14吗?).

I would recommend boost::filesystem, because it is planned to be added to STL (C++14 ?).

此处的文档: boost ::文件系统: :copy_file().

这篇关于在Linux上的C ++中移动文件的更快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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