- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请帮忙。我有一个名称为:
X_ab_LOTA123.png
然后我有了这段代码,我可以从文件名中提取数字 123。一切正常。
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/lexical_cast.hpp>
namespace bfs = boost::filesystem;
namespace ba = boost::adaptors;
using std::string;
using namespace std;
void find(const bfs::path& imageDirPath, const boost::regex& filter)
{
boost::smatch result;
for (auto& entry : boost::make_iterator_range(bfs::directory_iterator(imageDirPath), {})
| ba::filtered(static_cast<bool (*)(const bfs::path &)>(&bfs::is_regular_file))
| ba::filtered([&](const bfs::path &path){ return boost::regex_match(path.filename().string(), result, filter); })
)
{
int32_t num = boost::lexical_cast<int32_t>(result[1]);
cout << "found : " << num << " = " << entry.path().string() << endl;
}
}
int main()
{
const boost::regex defaultFilter( "X_ab_LOTA(\\d{3}).png" );
const bfs::path path(".");
find(path, defaultFilter);
}
结果是:
found : 123 = .\X_ab_LOTA123.png
但是,如果我将文件重命名为:
X_a_LOTA123.png
即我删除了“b”字母,并将过滤器相应地更改为:
const boost::regex defaultFilter( "X_a_LOTA(\\d{3}).png" );
然后程序崩溃了!
我得到了 bad_cast 异常!
我在这里做错了什么?代码很简单。但它仅适用于某些情况,对于其他名称和匹配模式它会崩溃。
请帮忙。
最佳答案
你在那里有未定义的行为。您的范围过滤器有副作用,但在您检查结果之前,副作用可能已被另一个范围条目覆盖。
因此,当 result[1]
可能无效(存在不匹配)或者它甚至可能引用另一条路径时,您正在使用它。更糟糕的是,在这种情况下,第二个过滤器表达式采用的 path const&
引用已经过时(它当时来自 entry
循环变量!)。
最糟糕的是:一旦你编写了 path.filename().string()
,你的代码就注定失败了,因为它返回一个临时的,并且它在封闭的完整表达式的末尾被销毁.
Running your code with debug-iterators or with address sanitizers would likely have alerted you about these problems
只是简化:
void find(const bfs::path& imageDirPath, const boost::regex& filter)
{
for (auto entry : bfs::directory_iterator(imageDirPath)) {
if (!is_regular_file(entry))
continue;
auto fname = entry.path().filename().string(); // keep alive longer than result!
boost::smatch result;
if (boost::regex_match(fname, result, filter)) {
int32_t num = boost::lexical_cast<int32_t>(result[1]);
cout << "found : " << num << " = " << entry.path().string() << endl;
}
}
}
事实上,您可能会添加检查匹配组 [1]
是否有效并且在使用它之前是否已成功匹配,因为您的函数不控制所使用的正则表达式。
关于c++ - 使用 boost::filesystem 和 boost::lexical_cast 从文件名中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45515195/
有没有办法确定我是否可以使用标准 (在所有支持 C++17 的现代 C++ 编译器上可用)或 由较旧的编译器使用。 (例如 g++ 6.3,这是 Debian Stretch 上的当前标准版本) 知
因此,boost::filesystem 允许您在文件所有者拥有的权限、组拥有的权限以及所有用户拥有的权限的意义上访问文件的权限。这很好,但我不想开始检查我是谁,我的组是什么等等——我只想检查我是否可
我不小心使用 rm -rf 删除了错误的文件夹,我尝试过的每个工具都告诉我我没有硬盘或找不到文件系统。 当我输入 df 时,我得到: 已使用的文件系统大小 Avail Use% Mounted on
我是 Java 的新手,正在尝试学习 IO 的概念。我遇到过两个非常相似的 Java 类,FileSystem 和 FileSystems。它们之间有什么区别?什么时候使用一个而不是另一个? 最佳答案
我查了很多关于c++17下文件系统链接的问题,还是无法链接成功。我的main.cpp文件如下。 #include int main(int argc, char** argv) { std:
Boost 库有一个类来处理文件路径:boost::filesystem::path。Boos 也有这个类 boost::filesystem::wpath 每个类都有方法string(), wstr
我正在编写一个利用 std::filesystem 的库(仅供学习)。它在 MSVC 上运行良好,但是默认情况下,Linux 的 LTS 版本就像 Ubuntu 一样附带 GCC 6.x,官方存储库中
请查找随附的代码片段。我正在使用此代码将文件从 hdfs 下载到我的本地文件系统 - Configuration conf = new Configuration(); FileSys
这段代码中std::filesystem::copy()和std::filesystem::copy_file()有什么区别? #include void testing() { const
以下代码旨在去除路径的第一部分,以防它存在: #include std::filesystem::path strip_prefix(std::filesystem::path p) {
在以下两种情况下,是否有理由调用lexically_normal: std::filesystem::path filepath = someFuntionThatGetsAPath(); filep
函数 boost::filesystem::canonical() ( doc of 1.66 , doc of current release ) 提供两个参数(忽略错误代码重载)base。第一个是
boost::filesystem::path使用 &转义路径字符串中的引号,see demo : std::cout 标题。 最佳答案 Boost::Filesystem 相当古老,早于 C++14
考虑以下关于路径分解的断言,其中每个局部变量,例如stem 具有明显的初始化,例如auto stem = path.stem() — assert(root_path == root_name / r
给定以下代码: fs::path p{ "a/b/" }; fs::path q{ "a/b/." }; assert(p == q); [注意定义 q 的字符串末尾的额
由于 C++17 std::filesystem 与 boost::filesystem 非常相似,所以我尝试做与这个问题相同的事情: Escaping some Directories in ite
我找到了这个页面,描述了 c++14 和 c++17 之间的变化: https://isocpp.org/files/papers/p0636r0.html ... 它链接到此页面,该页面描述了建议的
我有一些代码,当我编译它时,出现以下错误,我不知道如何解决。我尝试添加 -L/usr/lib/x86_64-linux-gnu、-lboost_system 和 -lboost_filesystem,
尝试使用 Asset.loadAsync 将 .txt Assets 作为字符串加载到 Expo 中 Asset.loadAsync(module) 解析并提供一个 localUri 但是,FileS
我在 中编码C++ 在 Visual Studio (Windows 10)并收到此错误: #error The header providing std::experimental::filesy
我是一名优秀的程序员,十分优秀!