|
Boost Users : |
From: Paul Giaccone (paulg_at_[hidden])
Date: 2005-10-04 11:35:34
How do you close a filtering_ostream object once you have finished
writing to it?
In my program below, I would expect the file "hello.bz2" to have been
written once the close() function has been called, but it is not the
case. The contents are not actually written until I exit the scope in
which the filtering_ostream object is defined (at which point the
destructor presumably does the job). (The file "some_text.txt" contains
just the text "hello world!" so I would expect it not to be written
until the file is closed.)
I would like to be able to do this explicitly and not have to rely on
the destruction of the object - could you explain how can this be done?
Another question: what should the second parameter to close() be? I've
used BOOST_IOS::trunc here, but is there some other more appropriate value?
Thanks,
Paul Giaccone
#include <fstream>
#include <iostream>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
int main(void)
{
{
boost::iostreams::filtering_ostream out;
out.push(boost::iostreams::bzip2_compressor());
out.push(boost::iostreams::file_sink("hello.bz2", BOOST_IOS::trunc));
std::ifstream input_stream("some_text.txt");
boost::iostreams::copy(input_stream, out);
input_stream.close();
if (!boost::iostreams::flush(out))
{
std::cerr << "failed to write file" << std::endl;
}
boost::iostreams::close(out, BOOST_IOS::_Dummy_enum_val);
} //exiting scope closes the stream; close() seems not to
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::bzip2_decompressor());
in.push(boost::iostreams::file_source("hello.bz2"));
boost::iostreams::copy(in, std::cout);
std::cout.flush();
return 0;
}
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