R6010-当boost文件系统的重命名或copy_file方法被命中时,Abort会被击中

时间:2014-02-11 12:54:40

标签: c++ file boost

描述: 我试图将目录中的所有文件移动到某个(用户选择的目录),基于它们通过boost文件系统扩展到某个目录。 问题: 当boost文件系统的rename / copy_file方法被命中时,我收到名为error的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)                                         
        }

    }
}

如果我遗漏了上述代码中的内容,请告诉我。谢谢。

此致 拉维

1 个答案:

答案 0 :(得分:0)

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