|
Boost Users : |
Subject: Re: [Boost-users] Read/write with gzip
From: Anders Knudby (knudby_at_[hidden])
Date: 2011-04-28 10:08:30
On Wed, Apr 27, 2011 at 11:45 PM, Ted Byers <r.ted.byers_at_[hidden]> wrote:
> Sorry for top posting (I don't know yet how to tell MS Outlook not to do
> so)
>
>
>
> Anyway, All I wanted to say is although I'd thought the filtering stream
> was derived from regular iostreams, it appears that isn't the case, and
> that's why close isn't available as a member function. However,
> boost::iostreams provides a template function called close, so the
> statements 'close(out)' and 'close(in)' both compile.
>
>
>
> I can't get it to link, though, as it complains about unresolved externals
> relating to libboost-zlib
.. this is with both MSVC++ 2010 and cygwin.
> There are no libraries related to zlib in either directory where I built the
> boost libraries. I don't (yet) know why.
>
>
>
> Anyway, your problem of closing your streams ought to be resolved by
> resorting to the close template function.
>
>
>
> Cheers
>
>
>
> Ted
>
>
>
> *From:* boost-users-bounces_at_[hidden] [mailto:
> boost-users-bounces_at_[hidden]] *On Behalf Of *Anders Knudby
> *Sent:* April-27-11 2:06 PM
> *To:* boost-users_at_[hidden]
> *Subject:* Re: [Boost-users] Read/write with gzip
>
>
>
>
>
> On Wed, Apr 27, 2011 at 12:27 PM, Ted Byers <r.ted.byers_at_[hidden]> wrote:
>
>
>
> On Tue, Apr 26, 2011 at 8:54 PM, Anders Knudby <knudby_at_[hidden]> wrote:
>
> Hello, I previously managed to write data to a gzipped file using the
> following code structure (it's an example of course, the "memblock" is
> actually filled with real data and the real filename is specified
> elsewhere).
>
> #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>
>
> namespace io = boost::iostreams;
>
> int size = 1000;
>
> char* memblock = new char [size];
>
> io::filtering_ostream out;
>
> out.push(io::gzip_compressor());
>
> out.push(io::file_descriptor_sink("c:/outfile.gz"));
> out.write (memblock, size);
>
>
>
> Is there a reason not to invoke the stream's member function 'close' at
> this point? You are, after all, reopening the same file later in your
> program.
>
> Cheers
>
> Ted
>
>
> Thanks for the help. The problem of writing the file and then trying to
> read it later in the same process is an artefact of my example, in reality I
> have one program to write the files, and another to read them again. But the
> reason I'm not specifically closing the filtering_stream is that I don't
> know how! I thought I could treat it as a normal iostream (e.g. using read,
> write etc.), but the normal closing of an iostream - in.close() - doesn't
> compile. Instead it gives me the following error: "error C2039: 'close' : is
> not a member of 'boost::iostreams::filtering_stream<Mode>". I have found a
> mention somewhere that I need to make the filtering_stream 'closeable', but
> I haven't found out how to do that. Any help would be appreciated, I do like
> to close my streams...
>
> In the meantime, I have somehow managed to get my code to work. I'm not
> sure how, but it is definitely related to the closing or not of the
> filtering_stream. If someone has a simple example of how to create and then
> close/destroy a filtering_streambuf that would be great. I can't find a
> single example in the Boost documentation, but maybe I'm just not looking in
> the right place or sufficiently code-literate.
>
> Anders
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
Interesting. I just tried close(in) and it compiles and runs without
problems. I swear that I have tried that previously and it gave me errors,
saying something along the lines that 'close' isn't part of
boost::iostreams. In my current implementation I use it as part of std (no
io:: prefix):
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/filesystem.hpp>
using namespace std;
namespace io = boost::iostreams;
int main()
{
int size = 6; //This is the uncompressed size of the file below
char* memblock = new char [size];
io::filtering_istream in;
in.push(io::gzip_decompressor());
in.push(io::file_descriptor_source("e:/my_file.gz"));
in.read(memblock, size);
close(in);
return 0;
}
Ted, to get zlib libraries built as part of boost, check out this page
http://stackoverflow.com/questions/2629421/how-to-use-boost-in-visual-studio-2010.
With some modifications that's how I got it to work in Visual Studio 2008.
It seems silly to me that iostreams, the filesystem and zlib and bzip2
aren't included in the standard build of Boost for Windows, but there's
probably a reason.
Anders
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