Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to manange files of un-known numbers with STL?

Reply
Thread Tools

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

 
 
peter.xiau@gmail.com
Guest
Posts: n/a
 
      11-18-2005
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:fstream when I need, and push the
std:fstream object to a vector std::vector<std:fstream>.
But I faced one problem that, the std: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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-18-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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:fstream when I need, and push the
> std:fstream object to a vector std::vector<std:fstream>.
> But I faced one problem that, the std: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: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
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      11-18-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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:fstream when I need, and push the
> std:fstream object to a vector std::vector<std:fstream>.
> But I faced one problem that, the std: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:fstream objects?
std::vector<std: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.


 
Reply With Quote
 
kerzum@mail.ru
Guest
Posts: n/a
 
      11-18-2005
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: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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
win32com ppt embedded object numbers reverting back to original numbers Lance Hoffmeyer Python 2 07-26-2007 01:45 AM
Fibonacci Numbers and Lucas Numbers Andrew Tatum C++ 6 05-27-2007 12:47 AM
Finding 1000 largest numbers from a file having some billion numbers Subra C Programming 25 03-08-2007 01:31 AM
Frame Numbers vs. JPEG Numbers With CF Cards mort Digital Photography 3 02-16-2005 01:43 AM



Advertisments