By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

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

P: n/a
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::ofstream when I need, and push the
std::ofstream object to a vector std::vector<std::ofstream>.
But I faced one problem that, the std::ofstream 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 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
pe********@gmail.com 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::ofstream when I need, and push the
std::ofstream object to a vector std::vector<std::ofstream>.
But I faced one problem that, the std::ofstream 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::ofstream > 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
Nov 22 '05 #2

P: n/a
<pe********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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::ofstream when I need, and push the
std::ofstream object to a vector std::vector<std::ofstream>.
But I faced one problem that, the std::ofstream 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::ofstream objects?
std::vector<std::ofstream*>

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.
Nov 22 '05 #3

P: n/a
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::ostream> > 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 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.