如何按文件扩展名引用文件夹中的文件
How to refer to files in folder by file extension
我正在将一个表示文件夹路径的字符串作为参数传递给构造函数。
文件夹名称、其中的文件及其名称可能会有所不同,但两个文件的扩展名始终相同:*.caffemodel
和*.prototxt
.
如何使用扩展名引用指定目录中的这两个文件,而无需扫描整个文件夹以获取其完整名称?
举个例子,这是我类的代码:
#include "NeuralClassifier.hpp"
NeuralClassifier::NeuralClassifier(std::string path)
{
std::string modelName = path + "/*.caffemodel";
std::string protoName = path + "/*.prototxt";
cv::dnn::Net net;
try {
net = dnn::readNetFromCaffe(protoName, modelName);
}
catch (cv::Exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
if (net.empty())
{
std::cerr << "Can't load network by using the following files: " << std::endl;
std::cerr << "prototxt: " << protoName << std::endl;
std::cerr << "caffemodel: " << modelName << std::endl;
std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
exit(-1);
}
}
}
在某些时候,操作系统需要知道应用程序要打开的确切文件名。在您的情况下,您指定一个通配符"*",该通配符可以不与多个文件匹配。
如果你只想打开与通配符匹配的第一个文件,你可以使用 boost::filesystem 来简化文件处理:
我可以使用掩码在带有 Boost 的目录中迭代文件吗?
相关文章:
- 如何将更多文件夹添加到c++include路径
- 从对象文件(.o)在macos上制作归档文件(.a文件)
- 有没有办法读取C++中的文件文件夹?
- 错误C1083:无法打开包含文件.文件正确包含在代码中
- NDK 无法看到头文件 .h 文件
- 文件文件夹关联或归属
- 如何以阻止的方式删除文件/文件夹
- 在MFC中序列化为Unicode文本文件-文件头错误
- (Qt C++)如何从QTableView将所选文件/文件夹打印到文本文件中
- 获取带有POCO的服务可执行文件文件夹
- 如何从文件文件夹创建自定义文件类型
- 如何在windows7有限内存的电脑上读取大文件?文件大小为25GB,但RAM容量只有16GB
- c++以二进制方式读取文件.文件结束的问题
- 如何在linux的makefile中使用头文件(.h)文件中定义的宏定义
- 如何从文件夹中读取文件?文件夹包含不同类型的文件名
- 如何查找指向文件/文件夹的所有硬链接和符号链接(Windows和UNIX)
- 生成临时文件/文件夹 c++ GTEST
- 更改应用程序文件(. c文件)的名称
- 使用vc++2010从usb读取文件/文件夹
- 为特定的文件/文件夹禁用-Wall标志