使用 Boost 列出文件夹中的文件时出错
error listing files in a folder using Boost
我开始在我的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"行是发生分段错误的地方。
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 在 C/C++ 中打开驻留在 "/sys/" 下的文件时出错
- 在头文件中定义变量不会出错
- 如何在多写入器情况下对文件支持的共享内存中的大页面出错
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 生成文件中隐式规则中的 -c 标志出错
- 编译时出错 - 链接.cpp和头文件
- 从人员矢量插入和读取文件时出错
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- ./main:加载共享库时出错:libopencv_highgui.so.4.0:无法打开共享对象文件:没有这样的文件或
- 猫鼬 - 包含标准C++库文件时出错
- 在f2c.h文件中定义min()max()宏时出错
- ASIOSDK;从hostsample.cpp生成mex文件时出错
- 使用emscripten将c++文件转换为wasm时出错
- 编译后可执行文件出错
- 头文件C++出错
- ftell 非常大的文件出错
- 从磁盘读取映像文件出错