簡體   English   中英

R6010-當boost文件系統的重命名或copy_file方法被命中時,Abort被命中

[英]R6010- Abort gets hit when rename or copy_file method of boost filesystem gets hit

說明:我正在嘗試通過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而未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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM