How to manange files of un-known numbers with STL?

Discussion in 'C++' started by peter.xiau@gmail.com, Nov 18, 2005.

  1. Guest

    In my case, I'd to open several output files at one time, but the
    number of output files are determined at run-time, not compile-time.

    So I tried to open file with std::eek:fstream when I need, and push the
    std::eek:fstream object to a vector std::vector<std::eek:fstream>.
    But I faced one problem that, the std::eek:fstream doesn't support a
    asignment operator, so vector.push_back (ostream) cause an compile-time
    error.

    Does there are other ways that I can manange multiple files at
    run-time?

    Thanks a lot :)
     
    , Nov 18, 2005
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    wrote:

    > In my case, I'd to open several output files at one time, but the
    > number of output files are determined at run-time, not compile-time.
    >
    > So I tried to open file with std::eek:fstream when I need, and push the
    > std::eek:fstream object to a vector std::vector<std::eek:fstream>.
    > But I faced one problem that, the std::eek:fstream doesn't support a
    > asignment operator, so vector.push_back (ostream) cause an compile-time
    > error.
    >
    > Does there are other ways that I can manange multiple files at
    > run-time?


    There is a standard trick to use a reference counted smart pointer to turn
    unique objects into container suitable things. Maybe you can start from the
    following proof of concept:


    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <boost/shared_ptr.hpp>

    template <typename StreamType>
    struct stream_handle {

    typedef StreamType stream_type;

    private:

    typedef boost::shared_ptr< stream_type > stream_ptr;

    stream_ptr my_ptr;

    public:

    stream_handle ( void )
    : my_ptr ( new stream_type () )
    {}

    stream_handle ( stream_handle const & other )
    : my_ptr ( other.my_ptr )
    {}

    ~stream_handle ( void ) {}

    stream_handle& operator= ( stream_handle const & other ) {
    this->my_ptr = other.my_ptr;
    return ( *this );
    }

    template < typename A >
    stream_handle ( A const & a )
    : my_ptr ( new stream_type ( a ) )
    {}

    template < typename A, typename B >
    stream_handle ( A const & a, B const & b )
    : my_ptr ( new stream_type ( a, b ) )
    {}

    template < typename A >
    stream_handle & operator<< ( A const & a ) {
    (*my_ptr) << a;
    return ( *this );
    }

    template < typename A >
    stream_handle & operator>> ( A & a ) {
    (*my_ptr) >> a;
    return ( *this );
    }

    }; // stream_handle<>


    int main ( void ) {
    typedef stream_handle< std::eek:fstream > o_handle;
    std::vector< o_handle > handle_vect;
    o_handle out ( "/dev/stdout" );
    handle_vect.push_back( out );
    handle_vect[0] << "hello world";
    o_handle out2;
    out2 = out;
    out2 << '\n';
    }


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 18, 2005
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    > In my case, I'd to open several output files at one time, but the
    > number of output files are determined at run-time, not compile-time.
    >
    > So I tried to open file with std::eek:fstream when I need, and push the
    > std::eek:fstream object to a vector std::vector<std::eek:fstream>.
    > But I faced one problem that, the std::eek:fstream doesn't support a
    > asignment operator, so vector.push_back (ostream) cause an compile-time
    > error.
    >
    > Does there are other ways that I can manange multiple files at
    > run-time?
    >
    > Thanks a lot :)


    How about just storing the pointers to std::eek:fstream objects?
    std::vector<std::eek:fstream*>

    so the it would be a vector.push_back(ostream*) which shouldn't cause you
    any problem.

    Of course then you have to deal with new and delete and the syntax of
    pointers, but if that's not a problem for you it should be okay.
     
    Jim Langston, Nov 18, 2005
    #3
  4. Guest

    Or even better use boost::shared_ptr pointers that allow you to 'add
    value semantic' to a pointer, e.g.:

    #include <fstream>
    #include <boost/shared_ptr.hpp>

    int main(void)
    {
    using namespace std;
    typedef std::vector<boost::shared_ptr<std::eek:stream> > vec_t;
    vec_t v;

    v.push_back(new ofstream("f1.txt"));
    v.push_back(new ofstream("f2.txt"));
    v.push_back(new ofstream("f3.txt"));
    v.push_back(new ofstream("f4.txt"));

    // eno need to delete anything - as v gets out of scope all streams
    are
    // automagically closed and deleted
    return 0;
    }
     
    , Nov 18, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Susanne Kaufmann

    cut and join mp3-files with known bitrate

    Susanne Kaufmann, Jun 28, 2006, in forum: Java
    Replies:
    6
    Views:
    478
    Chris Uppal
    Jun 30, 2006
  2. Subra
    Replies:
    25
    Views:
    1,262
    user923005
    Mar 8, 2007
  3. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    615
    Howard
    May 27, 2007
  4. Lance Hoffmeyer
    Replies:
    2
    Views:
    551
    Lance Hoffmeyer
    Jul 26, 2007
  5. Bret Thompson

    Downloading files of known mime type

    Bret Thompson, Jul 8, 2005, in forum: ASP General
    Replies:
    3
    Views:
    216
    Bret Thompson
    Jul 8, 2005
Loading...

Share This Page