- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在下面的代码片段中,有没有办法处理ENOSPC
?
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
// open input file stream of the bzip2 file
std::ifstream ifs("file.bz2");
// open output stream to the "full" device
// full device is a "utility-device" to check how applications handle ENOSPC
// more details in "man full"
std::ofstream ofs("/dev/full");
// Setup the iostreams filter
boost::iostreams::filtering_streambuf<boost::iostreams::output> filters;
filters.push(boost::iostreams::bzip2_decompressor());
filters.push(ofs);
// "run" the filter
boost::iostreams::copy(ifs, filters);
如果我对已编译的二进制文件执行 strace
,代码似乎会用相同的数据无限调用 writev()
并返回 ENOSPC
错误.
writev(4, [{NULL, 0}, {"DATA DATA "..., 4096}], 2) = -1 ENOSPC (No space left on device)
如何处理此错误或将其作为 boost::iostreams::copy()
的错误抛出。
是否可以在 ofstream
对象上设置适当的 exceptions()
?我尝试了 ofs.exceptions(std::ios::badbit | std::ios::failbit)
但它没有任何区别。
上面的代码是用 GCC 编译并在 Linux 上运行的。 Boost 版本 1.55。
最佳答案
它陷入了non_blocking_adaptor<Device>::write(...)
的无限循环中:
std::streamsize result = 0;
while (result < n) {
std::streamsize amt =
iostreams::write(device_, s + result, n - result);
result += amt;
}
return result;
iostream::write(device_, ...
一直返回 0(所以 n 保持 n,amt 和结果保持 0)。
这似乎是 Boost IOstreams 中的错误。可能是在添加了对非阻塞(同步)IO 的初步支持时引入的。根据文档,这应该是一项正在进行的工作。
特别有启发性的是:http://www.boost.org/doc/libs/1_55_0/libs/iostreams/doc/guide/asynchronous.html
Filters
Filters are allowed to propagate temporary failure notifications: if a downstream Device consumes or produces fewer characters than requested by a Filter, and if as a result the Filter is not able to satisfy a read or write request, the Filter may return a value indicating that input or output is temporarily unavailable. It is hoped that this ability will suffice to allow the current Filter concepts to be used with both aynchronous and non-blocking i/o. However, in order to be useful with blocking i/o as well, a Filter must never return a temporary failure notification unless it has received a such notification from a downstream Device. This requirement is summarized by stating that Filters must be blocking-preserving. See Blocking.
(大胆的想法)似乎 IOStreams 通过将 E_NOSPC 条件转换为临时故障通知 违反了这一原则。
boost::iostreams::copy
源和目标间接设备的特殊情况。在这种情况下,两者都是间接的。现在特殊情况将水槽包裹在 non_blocking_adaptor
中.我不知道为什么,这似乎与从同一文档页面获取的以下一般建议相矛盾:
Streams and Stream Buffers
Although the Boost.Iostreams Filter and Device concepts can accommodate non-blocking i/o, the C++ standard library stream and stream buffer interfaces cannot, since they lack a means to distinguish between temporary and permanent failures to satisfy a read or write request. As a result, non-blocking Devices do not work properly with the templates stream, stream_buffer, filtering_stream and filtering_streambuf.
我尝试用 file_sink
替换文件和 file_source
相反,但没有任何变化。 :(
这是我简化的测试用例,它仍然重现了问题:
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
int main()
{
using namespace boost::iostreams;
file_source ifs("/dev/zero");
file_sink ofs("/dev/full");
filtering_streambuf<output> filters(ofs);
copy(ifs, filters);
}
也许您应该将此作为错误报告给开发人员/邮件列表。
关于c++ - boost::iostreams::copy - sink - ENOSPC(设备上没有剩余空间)错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20414577/
在 C++ 中包含头文件时,和...有什么区别 在 <> 标志中包含 .h 部分与不包括 .h 部分? #include 与 #include 将标题名称用双引号括起来还是用 符号括起来? #inc
最近我在阅读 Accelerated C++ 并从练习答案中发现了这个有趣的代码。这是完整的代码, #include "stdafx.h" #include "4_4.h" #include usi
我正在开发一个需要能够在标准 C++ 编译器和准标准编译器上编译的实用程序。代码可以而且将会被扔到几乎任何现有的 C++ 编译器上。 我正在寻找一种可靠且可移植地确定目标编译器是否支持带或不带 .h
如果有一个文件 foo.cpp,那么它通常有一个关联的头文件 foo.h,其中包含 foo.cpp 中定义的函数的所有声明。这样,所有其他使用 foo.cpp 中的函数的文件都可以只包含 foo.h
iostream 和 iostream.h 有什么区别? 最佳答案 iostream.h 已被提供它的编译器弃用,iostream 是 C++ 标准的一部分。 为了明确说明,当前 C++ 标准 (IN
我知道关于 之间的区别的问题和 之前有人问过。阅读这些答案后,我发现了以下差异 当然iostream.h已弃用,新的符合标准的编译器不支持它 iostream.h不包含 std 中的所有内容命名
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Difference between iostream and iostream.h 我的教授说了以下内容:
我需要构建一些我在办公室电脑上得到的旧代码,它有 gcc 4.4.5安装。我编辑了代码(删除 .h 或添加类似 的内容)以使它们保持最新,以便它们可以由 gcc 4.4.5 编译.但是,在看似成功编
我在学习C++的时候遇到了一个问题,在编译的时候遇到了错误。 详情如下: 最佳答案 您似乎没有在 MinGW 中安装 C++ 支持。如果您使用手动安装路径,请下载 gcc-c++ dev、dll 和
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: What is the difference between #include and #include
对于我当前的任务,我需要能够读/写(主要是基于文件的)比特流。虽然如果用标准 C/C++ 编码,这或多或少是一项微不足道的任务,但我喜欢通过重载和使用更通用的方法重写代码标准的 STL iostrea
这个问题在这里已经有了答案: Difference between iostream and iostream.h (3 个答案) 关闭 9 年前。 有什么区别 #include 和 #inclu
我正在尝试为 linux/MacOS 转换一个用 C++14 编写的应用程序。它使用 boost::filesystem,但不用于某些 iostream 操作。例如: boost::filesyste
示例: namespace boostio = boost::iostreams; boostio::stream memStream(arr); while (!memStream.eof())
我编写了以下简单的 C++ 程序: #include using namespace std; int main() { cout ^~~~~~~~~~ 1 error
我想在我的代码中将流公开为它们的标准等价物,以消除用户对 boost::iostreams 的依赖性.如果有必要,当然想有效地执行此操作而无需创建拷贝。我考虑过只设置 std::istream的缓冲区
#include in header files and #include only in cpp files 被认为是最佳实践。我正在尝试将大量 #include 从 header 移动到现有
#include #include #include #include using namespace std; int main() { int ival; while(ci
假设我得到一个 stringbuf,其中包含一些必须删除的特定字符序列的内容: std::stringbuf string_buff; std::iostream io_stream (&string
我有一个包含如下记录的文件 123 Tag 现在是所有好人都来帮忙的时候了 总是有一个数字和一些标签,后面跟着一系列单词。我想将数字提取为整数,将标记提取为字符串,将句子提取为字符串。我已经使用 ge
我是一名优秀的程序员,十分优秀!