标签: boost-iostreams

使用 boost 将 C *FILE 转换为 C++ iostream

我对 C++ 相当陌生,想要将 *FILE(例如由 popen() 返回)转换为 iostream,以便与 getline 等函数一起使用。我找到了以下代码http://fw-geekycoder.blogspot。 co.za/2011/06/how-to-convert-c-file-to-c-iostream.html,以及来自很多地方的类似代码,但编译器抱怨boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_sink> bis(fd);boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_sink> bis(fd);

\n\n
#include <iostream>\n#include <cstdio>\n#include <unistd.h>\n#include <boost/iostreams/device/file_descriptor.hpp>\n#include <boost/iostreams/stream.hpp>\n\nvoid write() {\n    FILE* fp = fopen("whatever.txt", "w");\n    if (fp == NULL) {\n        perror("fopen error");\n    }\n    int fd = fileno(fp);\n    boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_sink> bis(fd);\n    std::ostream os(&bis);\n    os << "Hello World!" << std::endl;\n\n    fclose(fp);\n}\n\nvoid read() {\n    FILE* fp = fopen("whatever.txt", "r");\n    if (fp == NULL) {\n        perror("fopen error");\n    }\n    int fd = fileno(fp);\n    boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_source> bis(fd);\n    std::istream is(&bis);\n    while (is) {\n …
Run Code Online (Sandbox Code Playgroud)

c++ boost iostream boost-iostreams

4
推荐指数
1
解决办法
1911
查看次数

使用 boost iostreams 读取和写入数组到压缩文件

我想将一个数组写入一个文件,边走边压缩。

后来,我想从那个文件中读取数组,边走边解压。

Boost 的 Iostreams 似乎是一个不错的方法,所以我构建了以下代码。不幸的是,输出和输入数据在最后比较不相等。但他们几乎做到了:

Output         Input
0.8401877284   0.8401880264
0.3943829238   0.3943830132
0.7830992341   0.7830989957
0.7984400392   0.7984399796
0.9116473794   0.9116470218
0.1975513697   0.1975509971
0.3352227509   0.3352229893
Run Code Online (Sandbox Code Playgroud)

这表明每个浮点数的最低有效字节正在发生变化,或者什么的。但是,压缩应该是无损的,因此这不是预期的或不希望的。是什么赋予了?

//Compile with: g++ test.cpp --std=c++11 -lz -lboost_iostreams
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <cstdlib>
#include <vector>
#include <iomanip>

int main() 
{
    using namespace std;
    using namespace boost::iostreams;

    const int NUM = 10000;

    std::vector<float> data_out;
    std::vector<float> data_in;
    data_in.resize(NUM);
    for(float i=0;i<NUM;i++)
      data_out.push_back(rand()/(float)RAND_MAX);

    {
      ofstream file("/z/hello.z", ios_base::out | ios_base::binary);
      filtering_ostream out;
      out.push(zlib_compressor());
      out.push(file);

      for(const auto d: data_out) …
Run Code Online (Sandbox Code Playgroud)

c++ compression boost boost-iostreams

4
推荐指数
2
解决办法
3147
查看次数

在windows上使用zlib编译boost

有没有关于如何在 Windows 上使用 zlib 编译 boost 的好教程。\n我查看了 boost 参考,但它很模糊并且不够。\n我确实下载了 zlib dll 和源代码并在 Visual Studio 中进行了参考。\n我的链接有错误

\n\n
gzip_decompressor();\n
Run Code Online (Sandbox Code Playgroud)\n\n

完整代码:

\n\n
using namespace boost::iostreams;\nusing namespace std;\nstd::ifstream file("hello.gz", std::ios_base::in | std::ios_base::binary);\nfiltering_streambuf < input > in;\nin.push(gzip_decompressor());\nin.push(file);\nboost::iostreams::copy(in, std::cout);\n
Run Code Online (Sandbox Code Playgroud)\n\n

我收到这个错误,

\n\n
\n

错误 11 错误 LNK2019:无法解析的外部符号“_declspec(dllimport) public: __thiscall\n boost::iostreams::detail::gzip_header::~gzip_header(void)”\n (__imp??1gzip_header@detail@iostreams@boost@ @QAE@XZ) 在函数“public: __thiscall\n boost::iostreams::basic_gzip_decompressor\n \\>::~basic_gzip_decompressor >(void)”中引用 (??1?$basic_gzip_decompressor@V?$allocator@ D@std@@@iostreams@boost@@QAE@XZ)\n \xe2\x80\x93

\n
\n

c++ boost zlib boost-iostreams

3
推荐指数
1
解决办法
5269
查看次数

如何在Boost :: Log中使用压缩器Boost :: Iostreams过滤器作为接收器

我正在尝试通过利用来即时压缩使用Boost Log库创建的日志文件boost::iostreams::gzip_compressor.因此,当我打电话时BOOST_LOG(),输出会即时压缩.这是我到目前为止所尝试的:

#include <fstream>
#include <iostream>

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>

#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/logger.hpp>

void init()
{
    // Construct the sink
    typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > text_sink;
    boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();

    boost::shared_ptr< std::ofstream > file = boost::make_shared< std::ofstream >( 
            "sample.gz", std::ios_base::out | std::ios_base::binary );
    boost::iostreams::filtering_ostream out;
    out.push( boost::iostreams::gzip_compressor() );
    out.push( *(file.get()) );

    for( int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

c++ boost iostream boost-iostreams boost-log

3
推荐指数
1
解决办法
1143
查看次数

如何使用 boost iostreams 流式解压

我正在使用 boost iostreams (1.64.0) 来解压缩 zlib 数据。我想做流式解压。这意味着压缩数据的大小不可预测。我编写了以下代码示例。

#include <sstream>
#include <string>
#include <iostream>

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>

int main() {
    // Compress
    std::stringstream sender;
    boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
    out.push(boost::iostreams::zlib_compressor());
    out.push(sender);
    sender << "Hello World";
    std::stringstream compressed;
    boost::iostreams::copy(out, compressed);

    // Decompress
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
    in.push(boost::iostreams::zlib_decompressor());
    in.push(compressed);
    std::istream is(&in);
    std::size_t const buf_size = 256;
    char buf[buf_size] = { '\0' };
#if 0
    is.getline(buf, buf_size); // works fine
#else
    std::size_t read_size = is.readsome(buf, buf_size);
    std::cout << "read_size:" << read_size << std::endl;
#endif
    // http://www.cplusplus.com/reference/ios/ios/rdstate/ …
Run Code Online (Sandbox Code Playgroud)

c++ boost zlib boost-iostreams

3
推荐指数
1
解决办法
1310
查看次数

使用boost :: iostreams读取特制的数据,然后基于该创建对象并将其附加到列表中

我有一个有趣的问题.假设我的文件行包含如下:

name1[xp,y,z321](a,b,c){text};//comment
#comment
name2(aaaa);
Run Code Online (Sandbox Code Playgroud)

我也有(简化)课程:

class something {
public:
 something(const std::string& name);
 addOptionalParam(const std::string& value);
 addMandatoryParam(const std::string& value);
 setData((const std::string& value);
};
Run Code Online (Sandbox Code Playgroud)

name对应于某些类构造函数的param名称.[]括号中列出的内容是可选的,in()是必需的,{}之间的所有内容都应该作为字符串.

对于第一行,应该使用"name1"作为名称调用构造函数; 3次调用addOptionalParam,每次用冒号分隔一次; 还有3次addMandatoryParam和setData with"text".

我可以弄清楚如何做评论,但其他一切都是我的错...

现在我需要一些好的建议如何(或者如果)这是可能的,如果我可以解决如何为简单对象做这些,我可以弄清楚如何处理所有额外的血腥细节,如语义正确性.

c++ serialization parsing boost boost-iostreams

2
推荐指数
2
解决办法
448
查看次数

使用boost iostreams过滤器(关闭和不可复制)

在询问有关crypto ++的问题后,我尝试使用boost iostreams来实现它.我制作了以下代码:

#include <iostream>
#include <cryptopp/sha.h>
#include <algorithm>
#include <boost/array.hpp>
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file.hpp>

template<typename hash>
class sha_output_filter : public boost::iostreams::output_filter
{
  hash _hash;
  char _digest[hash::DIGESTSIZE];
public:
  typedef char                                 char_type;
  typedef boost::iostreams::output_filter_tag  category;

  sha_output_filter() {}
  //sha_output_filter(const sha_output_filter &) = delete;
  sha_output_filter &operator=(const sha_output_filter &) = delete;

  template<typename Sink>
  bool put (Sink &dest, int c)
  {
    std::cout << "put" << std::endl;
    char _c = c;
    _hash.Update ((const byte *)&_c, 1);
    boost::iostreams::put (dest, c);
  } …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-iostreams c++11

2
推荐指数
1
解决办法
2845
查看次数

boost :: iostreams压缩的字符串长度

我有一个字符串(有一些固定长度),我需要压缩然后比较压缩长度(作为数据冗余的代理或作为Kolmogorov复杂度的粗略近似).目前,我正在使用boost :: iostreams进行压缩,这似乎运行良好.但是,我不知道如何获取压缩数据的大小.有人可以帮帮忙吗?

代码片段是

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/filesystem.hpp>
#include <string>
#include <sstream>

namespace io = boost::iostreams;

int main() {

  std::string memblock;

  std::cout << "Input the string to be compressed:";
  std::cin >> memblock;

  std::cout << memblock << std::endl;

  io::filtering_ostream out;
  out.push(io::gzip_compressor());
  out.push(io::file_descriptor_sink("test.gz"));
  out.write (memblock.c_str(), memblock.size());

  std::cout << out.size() << std::endl;

  return 0;

}
Run Code Online (Sandbox Code Playgroud)

c++ boost-iostreams

2
推荐指数
1
解决办法
1512
查看次数

如何使用boost :: iostreams管道进入std :: cout

我是新手,boost::iostreams所以这可能是微不足道的:

假设 namespace io = boost::iostreams;

这很有效

io::filtering_ostream out(std::cout);
out << "some\nstring\n";
Run Code Online (Sandbox Code Playgroud)

这很有效

std::string result;
io::filtering_ostream out(io::counter() | io::back_inserter(result));
out << "some\nstring\n";
Run Code Online (Sandbox Code Playgroud)

但这不编译

io::filtering_ostream out(io::counter() | std::cout);
out << "some\nstring\n";
Run Code Online (Sandbox Code Playgroud)

你怎么管std::cout

c++ boost iostream boost-iostreams

2
推荐指数
1
解决办法
1287
查看次数

在boost iostreamfiltering_ostream中,sync()、strict_sync()和flush()有什么区别?

考虑一个简单的计数过滤器:

class CountableOstreamFilter : public boost::iostreams::multichar_output_filter {
public:
    CountableOstreamFilter(): m_written(0) { 
    }

    template<typename Sink>
    std::streamsize write(Sink& dest, const char* s, std::streamsize n)
    {
            auto result  = boost::iostreams::write(dest, s, n);
            assert(n == result);
            m_written += result;
            return result;
    }

    inline std::streamsize writtenBytes() const {
        return m_written;
    }

private:
    std::streamsize m_written;
};
Run Code Online (Sandbox Code Playgroud)

并这样使用它:

boost::iostreams::filtering_ostream counted_cout;
counted_cout.push(CountableOstreamFilter());
counted_cout.push(std::cout);
counted_cout << "hello world";
Run Code Online (Sandbox Code Playgroud)

调用sync()、strict_sync() 或flush() 之间有什么区别?counted_cout.sync(); // 与此调用有什么不同 counted_cout.strict_sync(); // 调用 counted_cout.flush(); // 这个调用?

我使用的是boost 1.50.0

c++ boost iostream boost-iostreams

1
推荐指数
1
解决办法
1851
查看次数

为什么stringstream :: str()截断字符串?

我有stringstream对象.它是通过填补

stringstream ss;
boost::iostreams::copy(inp,ss);
Run Code Online (Sandbox Code Playgroud)

boost::iostreams::filtering_streambuf<boost::iostreams::input> inp;
Run Code Online (Sandbox Code Playgroud)

并实际上保持在其中的ungzipped文件.

现在,如果我将stringstream内容刷新到文件

std::ofstream ofs(path_to_file,std::ios_base::out|std::ios_base::binary);
ofs << ss.rdbuf();
Run Code Online (Sandbox Code Playgroud)

一切都好.文件中包含完整的正确数据.

但是,如果我而不是像这样刷新文件构造字符串

std::string s = ss.str();
Run Code Online (Sandbox Code Playgroud)

内容在中间某处被截断.它不是持久性错误,它显然取决于字符串缓冲区的内容.

内容是几种语言的HTML文件.

它能是什么?谢谢.

c++ string stringstream boost-iostreams

1
推荐指数
1
解决办法
4327
查看次数

使用CMake对boost :: iostreams :: mapped_file_source :: init()进行未定义的引用

将最小示例上的错误与Boost Iostreams链接起来.看起来我还没有与libboost_iostream链接,但是CMake报告找到了库,并且Boost的其他应用程序编译和链接没有任何问题.

使用Cmake进行构建:

cmake_minimum_required(VERSION 3.0)
project(mmap_example CXX)
set(TARGET mmap_example)

set(BOOST_MIN_VERSION "1.61.0")
set(Boost_ADDITIONAL_VERSIONS "1.61.0" "1.61")
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_ROOT ${MY_BOOST_DIR})

find_package(Boost ${BOOST_MIN_VERSION} COMPONENTS iostreams REQUIRED)

set(CMAKE_CXX_FLAGS "-std=c++11 -std=gnu++1y -pthread")
set(CMAKE_EXE_LINKER_FLAGS "-std=c++11 -std=gnu++1y -pthread")

file(GLOB SOURCES *.cpp)

include_directories(${Boost_INCLUDE_DIRS}) 

add_executable(${TARGET} ${SOURCES})

target_link_libraries(${TARGET} ${Boost_IOSTREAMS})
Run Code Online (Sandbox Code Playgroud)

C++本身:

#include <boost/iostreams/device/mapped_file.hpp>

namespace boost_io = boost::iostreams;

int main(int argc, char** argv) {

    boost_io::mapped_file_source file(argv[1]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

GCC输出:

Linking CXX executable mmap_example CMakeFiles/mmap_example.dir/mmap.cpp.o: In function boost::iostreams::mapped_file_source::mapped_file_source<char*>(char* const&, unsigned int, long long): mmap.cpp:(.text._ZN5boost9iostreams18mapped_file_sourceC2IPcEERKT_jx[_ZN5boost9iostreams18mapped_file_sourceC5IPcEERKT_jx]+0x43): undefined reference to boost::iostreams::mapped_file_source::init()

gcc(Debian 4.9.2-10)4.9.2

Cmake 3.0.2 …

c++ cmake boost-iostreams

1
推荐指数
1
解决办法
587
查看次数