c++ - R6010-当 boost 文件系统的重命名或 copy_file 方法被命中时,中止被命中

标签 c++ file boost

描述: 我正在尝试将目录中的所有文件移动到某个(用户选择的目录),基于它们通过 boost 文件系统扩展到某个目录。 问题: 当 boost 文件系统的 rename/copy_file 方法被命中时,我收到名为错误的 R6010-Abort 方法。 示例:

SourceDirectory:C:\Source\a.txt
DestinationDirectory:C:\Destination

执行后:

DestinationDirectory:C:\Destination\a.txt

代码:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"

#include "boost/algorithm/string/regex.hpp"
#include "boost/regex.hpp"

namespace fs = boost::filesystem;
using namespace std;

void categorizeFolder()
{
    //Source Folder
    std::string folderToCategorize;
    cout<<"Choose the folder you want to categorize:";
    cin>>folderToCategorize;
    cout << "The directory you have choosen is: " << folderToCategorize << endl;

    //Destination folder
    std::string newfolder;
    cout<<"Choose the folder you want to store your files:";
    cin>>newfolder;
    cout << "The directory you have choosen is: " << newfolder << endl;

    std::vector< std::string > all_matching_files;
    boost::filesystem::directory_iterator end_itr; 

    for( boost::filesystem::directory_iterator i( folderToCategorize ); i != end_itr;     ++i )
    {
        if( !boost::filesystem::is_regular_file( i->status() ) ) continue;          
        if( i->path().extension() == ".txt"  ) 
        {
            cout<<i->path().extension();//Printing File extension
            cout<<i->path();//Printing file path
            cout<<i->path().filename()<<endl;   //Printing filename
            fs::rename(i->path(), newfolder);//This would move the     file//Even tried fs::copy_file(i->path(), newfolder)                                         
        }

    }
}

如果我在上面的代码中遗漏了什么,请告诉我。提前致谢。

问候, 拉维

最佳答案

Linux 错误如下所示:

terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
  what():  boost::filesystem::rename: Is a directory: "/tmp/first/test.txt", "/tmp/second"

API 调用被命名为 rename 而不是的事实,例如moveToFolder,应该让您知道您需要在目标中提供完整路径名。

fs::rename(
     it->path(), 
     fs::path(newfolder) / it->path().filename()); 

修复它。

这是一个具有更好组织和错误处理的版本。如果目标目录不存在,它甚至会创建目标目录!

#include <boost/filesystem.hpp>
#include <iostream>
#include <string>

namespace fs = boost::filesystem;
using namespace std;

void categorizeFolder(fs::path folderToCategorize, fs::path newfolder)
{
    if (!fs::exists(newfolder))
        fs::create_directories(newfolder);

    if (!fs::is_directory(newfolder))
    {
        std::cerr << "Destination folder does not exist and could not be created: " << fs::absolute(newfolder) << "\n";
        return;
    }

    for(fs::directory_iterator it(folderToCategorize), end_itr; it != end_itr; ++it)
    {
        if(!fs::is_regular_file(it->status())) 
            continue;          
        if(it->path().extension() == ".txt") 
        {
            // std::cout << it->path().extension() << "\n";
            // std::cout << it->path()             << "\n";
            // std::cout << it->path().filename()  << "\n";
            fs::rename(it->path(), fs::path(newfolder) / it->path().filename()); // move the file
        }
    }
}

int main(int argc, const char *argv[])
{
    if (argc<3)
    {
        std::cout << "Usage: " << argv[0] << " folderToCategorize newfolder\n";
        return 255;
    }

    std::string const folderToCategorize = argv[1];
    std::string const newfolder = argv[2];

    std::cout << "The directory you have choosen is: " << folderToCategorize << endl;
    std::cout << "The directory you have choosen is: " << newfolder << endl;

    categorizeFolder(folderToCategorize, newfolder);
}

关于c++ - R6010-当 boost 文件系统的重命名或 copy_file 方法被命中时,中止被命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21702434/

相关文章:

c++ - 一个简单的 C++11 宏

c++ - 如何为 Solaris 和 HP-AIX 定义 _FILE_OFFSET_BITS & _LARGE_FILES 宏

c++ - 将 vector bool 复制到 CUDA 内存时出错

c - 结构体中的指针问题

windows - 如果应用程序未运行,如何删除文件?

c++ - 我如何遍历元组

boost - 如何创建一个简单的 boost::lambda 函数?

c++ - boost::以下代码的任意替换

c++ - Opengl macOS 失败

file - Gradle删除任务失败,出现 “unable to delete file”