问题
all
I am developing to compress and decompress with boost in c++.
my codes are below:
#include <stdio.h>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
char *txtFile = "D:/Temp/plainTest.txt";
char *txtFile2 = "D:/Temp/plainTest_.txt";
char *binFile = "D:/Temp/plainTest.bin";
// compress
std::ifstream inStream(txtFile, std::ios_base::in);
std::ofstream outStream(binFile, std::ios_base::out);
boost::iostreams::filtering_streambuf< boost::iostreams::input> in;
in.push( boost::iostreams::gzip_compressor());
in.push( inStream );
boost::iostreams::copy(in, outStream);
// decompress
std::ifstream inStream2(binFile, std::ios_base::in);
std::ofstream outStream2(txtFile2, std::ios_base::out);
boost::iostreams::filtering_streambuf< boost::iostreams::input> in2;
in2.push( boost::iostreams::gzip_decompressor());
in2.push( inStream2 );
boost::iostreams::copy(in2, outStream2); ---> this line gives me the following error
plainText.txt file contains those characters.
The following line is in error
std::ofstream ofs
The compiler would've spit out a warning (at least) if your file was called jest; but
This gives me an error.
First-chance exception at 0x75692EEC in C_test.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::iostreams::zlib_error> > at memory location 0x00D8E074.
If there is a handler for this exception, the program may be safely continued.
And memory at 0x75692EEC is like
0x75692EEC 8b 4c 24 54 33 cc e8 9d a2 ff ff 8b e5 5d c2 10 00 8b 45 10 83 f8 0f 77 18 89 44 24 10 c1 e0 02 50 51 8d 44 24 1c 50 e8 c9 ad ff ff 83 c4 0c eb c5 6a 0f 58 eb e3 90 90 90 90 90 8b ff 55 ?L$T3????..??]?..?E.??.w.?D$.??.PQ?D$.P???..??.??j.X????????.U
But the funny thing is, if I delete ofs from the plainText.txt like
The following line is in error
std::ofstream
The compiler would've spit out a warning (at least) if your file was called jest; but
The error goes away, and it will produce plainText2.txt perfect.
What have I done wrong?
EDIT I pointed the line gives me the error.
回答1:
I think you need to specify that the output stream is binary:
std::ofstream outStream(binFile, std::ios_base::binary);
Then, after writing, do outStream.close() (to make sure the compressed output is flushed; you could call flush() instead but close() is more certain to work on all filesystems). Then, open the file for reading with binary
as well. Note there is no need to specify std::ios_base::in
(out
), as it is the default for std::ifstream (ofstream).
来源:https://stackoverflow.com/questions/22263931/strange-error-with-boostiostreamsgzip-decompressor-in-c