|
Boost Users : |
Subject: Re: [Boost-users] [Iostreams] Zlib compression sometimes creates corrupt data on OSX
From: Jeff Flinn (jeffrey.flinn_at_[hidden])
Date: 2013-10-02 13:50:06
On 10/2/2013 11:07 AM, Michael Jung wrote:
> Hello,
>
> Iâm having the following problem with my program: If I save data on OSX
> using a boost::iostreams::filtering_ostream with a
> boost::iostreams::zlib_compressor() the resulting file is sometimes
> corrupted.
>
> The following example code reproduces the problem:
>
> #include <fstream>
> #include <iostream>
> #include <vector>
> #include <boost/iostreams/filtering_stream.hpp>
> #include <boost/iostreams/filter/zlib.hpp>
>
> int main(int argc, char** argv)
> {
> if (argc != 2) return 1;
> std::ifstream file(argv[1]);
Maybe add: , std::ios_base::binary
> if (file)
> {
> file.seekg(0, std::ios::end);
> std::streampos length = file.tellg();
> file.seekg(0, std::ios::beg);
>
> std::vector<char> buffer1(length);
> std::vector<char> buffer2(length);
>
> file.read(&buffer1[0], length);
>
> {
> std::ofstream ofs("test.dat", std::ios_base::binary);
> boost::iostreams::filtering_ostream fos;
> fos.push(boost::iostreams::zlib_compressor());
> fos.push(ofs);
>
> fos.write(&buffer1[0], length);
> }
> {
> std::ifstream ifs("test.dat", std::ios_base::binary);
> boost::iostreams::filtering_istream fis;
> fis.push(boost::iostreams::zlib_decompressor());
> fis.push(ifs);
>
> fis.read(&buffer2[0], length);
> }
>
> std::cout << (buffer1 == buffer2 ? "equal" : "not equal")
> << std::endl;
> }
> }
otherwise search the mailing list for a thread from back in July 2013:
"[iostreams] How can I use the Stream Insertion operator (<<) with a
filtering_streambuf?"
Jeff
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net