c++ - Boost library iostream::copy not working -
#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; }
i have installed boost in question: how use boost in visual studio 2010 ?
and error on boost::iostreams::copy(in, cout)
statement.
i'm new boost don't know if code right or wrong.
i think did every thing should do, , i'm lost.
edit
i have checked question: exceptions boost::iostreams::copy() .
and have changed test code know error get.
#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 corrupted cout << "compressed data stream corrupted"; } else if(error == boost::iostreams::bzip2::data_error_magic) { // compressed data stream not begin 'magic' sequence 'b' 'z' 'h' cout << "compressed data stream not begin 'magic' sequence 'b' 'z' 'h'"; } else if(boost::iostreams::bzip2::config_error) { // libbzip2 has been improperly configured current platform cout << "libbzip2 has been improperly configured current platform"; } } }
it says boost::iostreams::bzip2::config_error
.
does mean installed bzip2 filter wrong???
because error used
b2 --toolset=msvc-10.0 -sbzip2_source="c:\bzip2-1.0.6" --build-type=complete stage
i get:
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 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 not contain mpi-based parallel components. note: enable them, add "using mpi ;" 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: enable mpi support, add "using mpi ;" user-config.jam. note: suppress message, pass "--without-mpi" bjam. note: otherwise, can safely ignore message. warning: no python installation configured , autoconfiguration note: failed. see http://www.boost.org/libs/python/doc/building.html note: configuration instructions or pass --without-python note: suppress message , silently skip boost.python targets error: no best alternative 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 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 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 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 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 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 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 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 not contain mpi-based parallel components. note: enable them, add "using mpi ;" 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: enable mpi support, add "using mpi ;" user-config.jam. note: suppress message, pass "--without-mpi" bjam. note: otherwise, can safely ignore message. warning: no python installation configured , autoconfiguration note: failed. see http://www.boost.org/libs/python/doc/building.html note: configuration instructions or pass --without-python note: suppress message , silently skip boost.python targets error: no best alternative 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 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 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 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 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 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 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...
the error getting misleading.
this because:
if(boost::iostreams::bzip2::config_error)
...always evaluates non-zero , gives wrong error message!
i don't have windows pc available me, tested "your" code on mac (boost installed using homebrew), , got same error.
libbzip2 has been improperly configured current platform
and fixed , added error checking:
#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 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 corrupted"; } else if (error == bzip2::data_error_magic) { cout << "compressed data stream not begin 'magic' sequence 'b' 'z' 'h'"; } else if (error == bzip2::config_error) { cout << "libbzip2 has been improperly configured current platform"; } else { cout << "error: " << error; } cout << endl; } }
build
> clang++ 18121908.cpp -lboost_iostreams -lboost_system -lboost_filesystem -o 18121908
run
> ./18121908 can't find gcc-4.7.2.tar.bz2. expect errors follow! bzip2 error error: -7
why -7
? because #define bz_unexpected_eof (-7)
. boost calls bzip2::unexpected_eof
. you've unexpectedly hit end of stream!
make valid (if questionable) bzip2 file:
> echo 'there should compiler stuff in here' > gcc-4.7.2.tar > bzip2 gcc-4.7.2.tar
re-run
> ./18121908 there should compiler stuff in here
conclusion
you need check whether file exists before attempting decompress it.
Comments
Post a Comment