简体   繁体   English

在Linux上用c ++移动文件的更快捷方式

[英]Faster way to move file in c++ on linux

I'm trying to move files on linux by using C++. 我正在尝试使用C ++在linux上移动文件。 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;
}
//-----

Now my problem. 现在我的问题。 I realized, this method is very slow. 我意识到,这种方法很慢。 It takes 47 seconds for 100MB. 100MB需要47秒。 Simply copy a file with the console command takes 2-3 seconds. 只需使用console命令复制文件需要2-3秒。

Does anybody have an idea? 有人有想法吗?

Streams are known to be pretty slow. 众所周知,流很慢。 You can either use tools provided by operating system or you can use some portable wrapper. 您可以使用操作系统提供的工具,也可以使用一些便携式包装器。

I would recommend boost::filesystem , because it is planned to be added to STL (C++14 ?). 我建议使用boost::filesystem ,因为它计划添加到STL(C ++ 14?)。

Documentation here: boost::filesystem::copy_file() . 这里的文档: boost :: filesystem :: copy_file()

使用Linux - 重命名(旧名称,新名称);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM