【问题标题】:R6010- Abort gets hit when rename or copy_file method of boost filesystem gets hitR6010- 当 boost 文件系统的 rename 或 copy_file 方法被击中时,中止被击中
【发布时间】:2014-02-11 12:54:40
【问题描述】:

说明: 我正在尝试将目录中的所有文件移动到某个(用户选择的目录),基于它们通过 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)                                         
        }

    }
}

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

问候, 拉维

【问题讨论】:

    标签: c++ file boost


    【解决方案1】:

    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);
    }
    

    【讨论】:

    • 谢谢。我错过了 boost fs 的重命名方法的第二个参数。
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多