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

P

peter.xiau

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 :)
 
K

Kai-Uwe Bux

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
 
J

Jim Langston

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.
 
K

kerzum

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top