错误列出使用Boost的文件夹中的文件

时间:2016-02-04 13:15:17

标签: c++ boost iterator

我开始在我的C ++程序中使用Boost,但是使用此代码遇到错误(分段错误):

#include <iostream>
#include <vector>
#include <set>
#include "tools.h"
#include "Cycle.h"
#include "Base.h"
#include <boost/filesystem.hpp>

using namespace std;

using namespace boost::filesystem;

int main()
{
    path p("/home/malinou/workspace/grunbaum2/grunbaum/Bases/");
    directory_iterator end_itr(p),end;

    cout << "path = " << p.string() << endl;
    cout << "end iterator = " << end_itr->path().string() << endl;

    // cycle through the directory
    for (directory_iterator my_itr(p); my_itr != end_itr; ++my_itr)
    {
        // If it's not a directory, list it. If you want to list directories too, just remove this check.

        cout << "path iterator : " << my_itr->path().string() << endl;

        cout << "is regular = " << is_regular_file(my_itr->path()) << endl;
        if (is_regular_file(my_itr->path()) ) {
        // assign current file name to current_file and echo it out to the console.
            string current_file = my_itr->path().string();
            cout << current_file << endl;
        }
    }
    return 0;
}

我在我使用的文件夹中有2个文件(config.txt和test.txt)。迭代器给出的第一个文件是text.txt,但是is_regular_file函数返回false,并且在for循环中递增迭代器会导致分段错误(核心转储)。

知道发生了什么事吗?

结果是:

path = /home/malinou/workspace/grunbaum2/grunbaum/Bases/
end iterator = /home/malinou/workspace/grunbaum2/grunbaum/Bases/text.txt
path iterator : /home/malinou/workspace/grunbaum2/grunbaum/Bases/test.txt
is regular = 0
Segmentation fault (core dumped)

Process returned 139 (0x8B)

第二版代码:

#include <iostream>
#include <vector>
#include <set>
#include "tools.h"
#include "Cycle.h"
#include "Base.h"
#include <boost/filesystem.hpp>

using namespace std;

using namespace boost::filesystem;

int main()
{
    path p("/home/malinou/workspace/grunbaum2/grunbaum/Bases/");
    directory_iterator end_itr;

    cout << "path = " << p.string() << endl;
    cout << "end iterator = " << end_itr->path().string() << endl;

    // cycle through the directory
    for (directory_iterator my_itr(p); my_itr != end_itr; ++my_itr)
    {
        // If it's not a directory, list it. If you want to list directories too, just remove this check.

        cout << "path iterator : " << my_itr->path().string() << endl;

        cout << "is regular = " << is_regular_file(my_itr->path()) << endl;
        if (is_regular_file(my_itr->path()) ) {
        // assign current file name to current_file and echo it out to the console.
            string current_file = my_itr->path().string();
            cout << current_file << endl;
        }
    }
    return 0;
}

2 个答案:

答案 0 :(得分:0)

你应该从不取消引用结束迭代器。因此,你的名字也被误导了。

for (directory_iterator my_itr(p), end_itr; my_itr != end_itr; ++my_itr)

这是一个固定的简化版本:

<强> Live On Coliru

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

using namespace std;

using namespace boost::filesystem;

int main()
{
    path p(".");

    cout << "path = " << p.string() << endl;

    // cycle through the directory
    for (directory_iterator my_itr(p), end_itr; my_itr != end_itr; ++my_itr)
    {
        // If it's not a directory, list it. If you want to list directories too, just remove this check.

        cout << "path iterator : " << my_itr->path().string() << endl;

        cout << "is regular = " << is_regular_file(my_itr->path()) << endl;
        if (is_regular_file(my_itr->path()) ) {
        // assign current file name to current_file and echo it out to the console.
            string current_file = my_itr->path().string();
            cout << current_file << endl;
        }
    }
    return 0;
}

答案 1 :(得分:0)

感谢您的建议,

不幸的是它并没有解决我的问题。似乎调用函数is_regular_file会导致问题。 (顺便说一下,如果迭代器已找到文件,为什么需要检查文件是否“常规”?)。

这是导致我遇到同样错误的代码:

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

using namespace std;

using namespace boost::filesystem;

int main()
{
    path p("/home/malinou/workspace/grunbaum2/grunbaum/Bases");
    cout << "path = " << p.string() << endl;

    directory_iterator new_itr(p);  

    cout << "new_itr path : " << new_itr->path().string() << endl;

    if(is_regular_file(new_itr->path()))
       cout << "is regular" << endl;
    else
       cout << "is not regular" << endl;

    cout << "new_itr path : " << new_itr->path().string() << endl;
    return 0;
}

最后一个“cout”行是分段错误发生的地方。