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

error listing files in a folder using Boost

本文关键字:文件 出错 文件夹 Boost 使用      更新时间:2023-10-16

我开始在我的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 个文件(配置.txt和测试.txt)。迭代器给出的第一个文件是文本.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;
}

永远不要取消引用结束迭代器。因此,你的名字也被误导了。

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

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

住在科里鲁

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

感谢您的建议,

不幸的是,它不能解决我的问题。似乎调用函数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"行是发生分段错误的地方。