c++ - Boost 库 iostream::copy 不起作用

标签 c++ boost boost-iostreams

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>

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

    ifstream file("gcc-4.7.2.tar.bz2", ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(bzip2_decompressor());
    in.push(file);
    boost::iostreams::copy(in, cout);

    system("pause");
    return 0;
}

我已经按照这个问题安装了 Boost: How to use Boost in Visual Studio 2010

我在 boost::iostreams::copy(in, cout) 语句上遇到错误。

我对 Boost 很陌生,所以我不知道这段代码是对还是错。

我想我已经做了我应该做的一切,但我迷失了。


编辑

我查过这个问题:exceptions from boost::iostreams::copy() .

我已经更改了测试代码以了解出现了什么错误。

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>

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

    try {
        ifstream file("hello.bz2", ios_base::in | ios_base::binary);
        filtering_streambuf<input> in;
        in.push(bzip2_decompressor());
        in.push(file);
        boost::iostreams::copy(in, cout);
    }
    catch(const bzip2_error& exception) {
        int error = exception.error();

        if(error == boost::iostreams::bzip2::data_error) {
            // compressed data stream is corrupted
            cout << "compressed data stream is corrupted";
        }
        else if(error == boost::iostreams::bzip2::data_error_magic)
        {
            // compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'
            cout << "compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'";
        }
        else if(boost::iostreams::bzip2::config_error) {
            // libbzip2 has been improperly configured for the current platform
            cout << "libbzip2 has been improperly configured for the current platform";
        }
    }
}

它说我得到了 boost::iostreams::bzip2::config_error

这是否意味着我安装了 bzip2 过滤器错误???

因为我使用的内容出现了一些错误

b2 --toolset=msvc-10.0 -sBZIP2_SOURCE="C:\bzip2-1.0.6" --build-type=complete stage

我得到:

C:\boost_1_54_0>b2 --toolset=msvc-10.0 -sBZIP2_SOURCE="C:\bzip2-1.0.6" --build-t
ype=complete stage
Performing configuration checks

    - 32-bit                   : yes (cached)
    - arm                      : no  (cached)
    - mips1                    : no  (cached)
    - power                    : no  (cached)
    - sparc                    : no  (cached)
    - x86                      : yes (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <thre
ading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <th
reading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - has_icu builds           : no  (cached)
warning: Graph library does not contain MPI-based parallel components.
note: to enable them, add "using mpi ;" to your user-config.jam
    - zlib                     : no  (cached)
    - iconv (libc)             : no  (cached)
    - iconv (separate)         : no  (cached)
    - icu                      : no  (cached)
    - icu (lib64)              : no  (cached)
    - message-compiler         : yes (cached)
    - compiler-supports-ssse3  : yes (cached)
    - compiler-supports-avx2   : no  (cached)
    - gcc visibility           : no  (cached)
    - long double support      : yes (cached)
warning: skipping optional Message Passing Interface (MPI) library.
note: to enable MPI support, add "using mpi ;" to user-config.jam.
note: to suppress this message, pass "--without-mpi" to bjam.
note: otherwise, you can safely ignore this message.
warning: No python installation configured and autoconfiguration
note: failed.  See http://www.boost.org/libs/python/doc/building.html
note: for configuration instructions or pass --without-python to
note: suppress this message and silently skip all Boost.Python targets
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <thre
ading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <th
reading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <thre
ading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <th
reading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <thre
ading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <th
reading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
    - zlib                     : no  (cached)
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <thre
ading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <th
reading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <thre
ading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <th
reading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
    - zlib                     : no  (cached)
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <thre
ading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <th
reading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <thre
ading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <th
reading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)

Component configuration:

    - atomic                   : building
    - chrono                   : building
    - context                  : building
    - coroutine                : building
    - date_time                : building
    - exception                : building
    - filesystem               : building
    - graph                    : building
    - graph_parallel           : building
    - iostreams                : building
    - locale                   : building
    - log                      : building
    - math                     : building
    - mpi                      : building
    - program_options          : building
    - python                   : building
    - random                   : building
    - regex                    : building
    - serialization            : building
    - signals                  : building
    - system                   : building
    - test                     : building
    - thread                   : building
    - timer                    : building
    - wave                     : building

...patience...
...patience...
...patience...
...patience...
...patience...
...found 47439 targets...

C:\boost_1_54_0>b2 --toolset=msvc-10.0 -sBZIP2_SOURCE="C:\bzip2-1.0.6" --build-type=complete stage
Performing configuration checks

    - 32-bit                   : yes (cached)
    - arm                      : no  (cached)
    - mips1                    : no  (cached)
    - power                    : no  (cached)
    - sparc                    : no  (cached)
    - x86                      : yes (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <threading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <threading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - has_icu builds           : no  (cached)
warning: Graph library does not contain MPI-based parallel components.
note: to enable them, add "using mpi ;" to your user-config.jam
    - zlib                     : no  (cached)
    - iconv (libc)             : no  (cached)
    - iconv (separate)         : no  (cached)
    - icu                      : no  (cached)
    - icu (lib64)              : no  (cached)
    - message-compiler         : yes (cached)
    - compiler-supports-ssse3  : yes (cached)
    - compiler-supports-avx2   : no  (cached)
    - gcc visibility           : no  (cached)
    - long double support      : yes (cached)
warning: skipping optional Message Passing Interface (MPI) library.
note: to enable MPI support, add "using mpi ;" to user-config.jam.
note: to suppress this message, pass "--without-mpi" to bjam.
note: otherwise, you can safely ignore this message.
warning: No python installation configured and autoconfiguration
note: failed.  See http://www.boost.org/libs/python/doc/building.html
note: for configuration instructions or pass --without-python to
note: suppress this message and silently skip all Boost.Python targets
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <threading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <threading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <threading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <threading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <threading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <threading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
    - zlib                     : no  (cached)
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <threading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <threading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <threading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <threading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
    - zlib                     : no  (cached)
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <threading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <threading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)
error: No best alternative for libs/coroutine/build/allocator_sources
    next alternative: required properties: <link>static <target-os>windows <threading>multi
        not matched
    next alternative: required properties: <link>static <segmented-stacks>on <threading>multi
        not matched
    next alternative: required properties: <link>static <threading>multi
        not matched
    - zlib                     : no  (cached)

Component configuration:

    - atomic                   : building
    - chrono                   : building
    - context                  : building
    - coroutine                : building
    - date_time                : building
    - exception                : building
    - filesystem               : building
    - graph                    : building
    - graph_parallel           : building
    - iostreams                : building
    - locale                   : building
    - log                      : building
    - math                     : building
    - mpi                      : building
    - program_options          : building
    - python                   : building
    - random                   : building
    - regex                    : building
    - serialization            : building
    - signals                  : building
    - system                   : building
    - test                     : building
    - thread                   : building
    - timer                    : building
    - wave                     : building

...patience...
...patience...
...patience...
...patience...
...patience...
...found 47439 targets...

最佳答案

您收到的错误实际上具有误导性。

这是因为:

if(boost::iostreams::bzip2::config_error)

...始终评估为非零并给您错误的错误消息!

我没有可用的 Windows PC,但我在 Mac 上测试了“您的”代码(使用 Homebrew 安装了 Boost),并得到了相同的错误。

libbzip2 has been improperly configured for the current platform

所以我修复了这个问题并添加了一些错误检查:

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/filesystem.hpp>

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

    char filename[] = "gcc-4.7.2.tar.bz2";

    if (!boost::filesystem::exists(filename))
    { 
        cout << "Can't find " << filename << ". Expect errors to follow! " << endl;
    } 

    ifstream file(filename, ios_base::in | ios_base::binary);

    filtering_streambuf<input> in;
    in.push(bzip2_decompressor());
    in.push(file);
    try
    { 
        boost::iostreams::copy(in, cout);
    } 
    catch (const bzip2_error& exception) 
    { 
        cout << exception.what() << endl;

        int error = exception.error();
        if (error == bzip2::data_error)
        { 
            cout << "compressed data stream is corrupted";
        } 
        else if (error == bzip2::data_error_magic)
        { 
            cout << "compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'";
        } 
        else if (error == bzip2::config_error) 
        {
            cout << "libbzip2 has been improperly configured for the current platform";
        } 
        else
        { 
            cout << "Error: " << error;
        } 
        cout << endl;
    } 
}

构建

> clang++ 18121908.cpp -lboost_iostreams -lboost_system -lboost_filesystem -o 18121908

运行

> ./18121908 
Can't find gcc-4.7.2.tar.bz2. Expect errors to follow! 
bzip2 error
Error: -7

为什么是-7?因为#define BZ_UNEXPECTED_EOF (-7)。 Boost 将此称为 bzip2::unexpected_eof。您意外地到达了流的结尾!

创建一个有效的(如果有问题的)bzip2 文件:

> echo 'There should be some compiler stuff in here' > gcc-4.7.2.tar
> bzip2 gcc-4.7.2.tar 

重新运行

> ./18121908 
There should be some compiler stuff in here

结论

在尝试解压该文件之前,您需要检查该文件是否存在。

关于c++ - Boost 库 iostream::copy 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18121908/

相关文章:

c++ - 如何仅使用指针将 C++ 数组中的每个元素向右移动一个?

c++ - Boost Write XML 错误编码

c++ - 将 boost filtering_stream 替换为 std::ofstream 的参数语法

c++ - 可以创建 N 个方法的类接口(interface)

c++ - 在 For 循环 C++ 中创建线程

c++ - 在类中使用私有(private)变量

c++ - boost::ptr_vector 和 find_if

c++ - 如何查找不包含任何平台特定代码的Boost库

c++ - 在使用 boost::asio 和 boost::iostreams(或其他东西?)进行异步非阻塞文件加载方面需要帮助

c++ - 使用 BOOST property_tree/iostreams/filesystem/foreach - 结果出现链接错误