Boost logo

Boost Users :

From: Aljaz (aljaz.fajmut_at_[hidden])
Date: 2007-03-21 11:57:36


Hello

I've been working with boost iostreams filters.
Since when I changed

  fin.push(file_source("outest.fil", ios::in | ios::binary));

to:

  file_source fss("outest.fil", ios::in | ios::binary);
  fin.push(boost::ref(fss));

file crashes when the task is done.
Why is that happening? What am I doing wrong? I think it has something to do
with memory allocation..

Another thing im unsure about:
Should I open file each time I want to read from it, or can I open it at
program start, and leave it open (program will be running for a long period)
?

I hope someone responds this time
Thanks for help
Best regards

#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/line.hpp>
#include <boost/ref.hpp>
#include <exception>
#include <fstream>
#include <ostream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace boost::iostreams;
namespace io = boost::iostreams;

class long_line_counter : public boost::iostreams::line_filter {
public:
 explicit long_line_counter(int max_length = 80)
  : max_(max_length), count_(0) { }

 int count() const { return count_; }

 vector<string> mail_list;

private:
 std::string do_filter(const std::string& line) {
  if (line.size() < max_) mail_list.push_back(line);
  ++count_;
  return line;
 }

 int max_;
 int count_;
};

int main() {
 try {
  filtering_istream fin;
  long_line_counter llc(80);

  file_source fss("outest.fil", ios::in | ios::binary);

  fin.push(boost::ref(llc));
  fin.push(boost::ref(fss));

  char buf[4012];
  while ( fin.read(buf, 4012) ) {
  }

  std::cout << llc.count() << "\n";
  std::cout << llc.mail_list.size() << "\n";
 }
 catch (exception &e) {
  cout << "exception: " << e.what() << std::endl;
 }

 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