- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
自从我使用 C++ 以来已经有一段时间了,请原谅我的新手问题。
我编写了以下代码来获取目录内容的列表,它运行良好:
for (directory_iterator end, dir("./");
dir != end; dir++) {
std::cout << *dir << std::endl;
}
“*dir”返回什么,一个“字符数组”指针,一个指向“字符串”对象的指针,还是一个指向“路径”对象的指针?
我想将“*dir”(如果它以 .cpp 结尾)传递给另一个 function(),它将在稍后(异步)对其进行操作。我想我需要复制“*dir”。我写了下面的代码:
path *_path;
for (directory_iterator end, dir("./");
dir != end; dir++) {
_path = new path(*dir);
if (_path->extension() == ".cpp") {
function1(_path); // function1() will free _path
} else
free(_path);
}
谢谢,艾哈迈德。
最佳答案
来自documentation of boost::directory_iterator :
The result of operator* on an end iterator is not defined. For any other iterator value a const directory_entry& is returned.
关于函数调用,我觉得最简单的方法是:
using namespace boost::filesystem;
for (directory_iterator end, dir("./"); dir != end; dir++) {
const boost::filesystem::path &this_path = dir->path();
if (this_path.extension() == ".cpp") {
function1(this_path); // Nothing to free
}
}
其中 function1 方法可以声明为:
void function1(const boost::filesystem::path this_path);
关于C++ directory_iterator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15955609/
自从我使用 C++ 以来已经有一段时间了,请原谅我的新手问题。 我编写了以下代码来获取目录内容的列表,它运行良好: for (directory_iterator end, dir("./");
我正在尝试遍历目录并读取其中的图像,同时允许跳过每第 n 个文件。 我的代码目前看起来像这样: // Count number of files in directory and reserve me
这是我的代码: #include #include int main(int argc, char *argv[]) { auto iter = std::filesystem::dire
编辑:我使用的是 std::tr2::sys 而不是 boost 的文件系统 我正在开发一个简单的应用程序,它可以扫描计算机上的目录并返回有关这些文件的统计信息。可以通过两种方式调用扫描:递归和非递归
我想重命名目录中的文件。目录中有 52 个文件夹。每个文件夹都有不同的名称,每个文件夹中有大约 40 个文件。我想提取特定文件夹的名称并将该名称附加到该特定文件夹中的文件名称。 当每个文件夹中只有 3
所以我写了一个小程序来试用 Boost Filesystem。我的程序将写入当前路径中有多少个文件,然后是文件名。这是我的程序: #include #include using namespace
我一直在尝试使用通过快速谷歌搜索找到的这个示例遍历目录: namespace bf = boost::filesystem; bf::path p("somedir"); bf::directory_
我已经在 directory_iterator page 上复制了示例代码,所以这就是我所拥有的: #include "pch.h" //for visual studios benefit #inc
我有以下代码: for (const auto& x : std::filesystem::directory_iterator(dir)) { // do stuff with x } di
我正在遍历一个目录,当某个项目符合某些条件时,我将其删除。我可以在循环内安全地执行此操作,还是必须将路径保存在数组中并稍后删除? 我在boost::filesystem docs中没有找到相关信息.
有些东西没有意义。根据我读过的内容,你使用 std::filesystem 是这样的: #include #include #include int main() { auto it
许多人(例如 1 、 2 )询问如何获得 std::filesystem::directory_iterator去工作,但在我读完这些之后我仍然遇到了麻烦。 我正在尝试构建一个小型静态库。在将目录迭代
我在 gcc 6.3.1 中使用实验性 std::filesystem 实现,遇到了一些关于 std::experimental::filesystem::directory_iterator #in
以下代码递归地遍历给定目录并每次以相同顺序打印其内容。 是否可以改变目录迭代器以随机方式打印目录内容(而不是使用 vector 存储结果然后随机打印 vector 内容)? #include #in
我正在编写一个程序,它获取目录中的所有文件名并将它们放入一个数组中。我遇到的问题是 operator++() 显示错误并且不会增加迭代器。任何帮助将不胜感激。 #include #include
我知道这听起来很愚蠢,但看看这个简单的例子(工作目录应该有多个项目): #define BOOST_FILESYSTEM_VERSION 3 #include #include int main(
我正在尝试使用其 insert(const_iterator pos, InputIt first, InputIt last) 将转换后的目录条目范围插入 vector 中成员函数模板。 不幸的是,
我正在尝试获取最后一个文件名。 下面的代码非常优雅。但它正在遍历目录中的所有文件。 是否可以在不迭代的情况下获取最后一个文件? #include std::string latestFileName
我正在使用 boost 的文件系统和 std::max_element() 来查找给定目录中名称最长的文件: #include #include #include #include #incl
我正在尝试学习如何使用 Boost C++ 库,但我遇到了一个问题。运行以下 boost::filesystem 教程代码时: #include #include using std::cout;
我是一名优秀的程序员,十分优秀!