473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
3 1313
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Agathe | last post by:
Bonjour, Je souhaite insérer dans une table MySQL des données provenant d'un fichier texte grâce à un script PHP. Mon fichier porte l'extension "txt" et les données sont séparées par des ";'. ...
9
by: Charley Kyd | last post by:
I'm a newbie who needs advice about how to use external files of JavaScript code. I spent an hour this afternoon browsing through JavaScript books at the local book store. In about 15 different...
3
by: pascal Joseph | last post by:
J'ai un formulaire avec un seul champ text appelé "unite" et un bouton. En javascript j'aimerai utiliser un script qui interdise les valeurs de type "char" et soit supérieur à 0 J'ai trouvé...
5
by: Chris | last post by:
Bonjour, Plusieurs fichiers PHP d'un programme open source de compteur de visites viennent de se faire hacker sur mon serveur (hébergement mutualisé chez un fournisseur d'accès). Le hacker a...
5
by: gwarning! | last post by:
Goal: Have multiple text files, each of various lengths, be concatenated together into one final consolidated text file. Problem: Since the names of the files to be concatenated can change from...
3
by: Jorge Gallardo | last post by:
Hola de nuevo a todos... Agradecido a todos los que me habeis solucionado problemas anteriores... Pero como no es novedad, me surge otro. Recientemente buscando, adquiri un codigo para juntar...
1
by: Alex | last post by:
Ciao a tutti, sto sviluppando un applicazione windows, in breve all'interno dello stesso namespace ho un form con una datagrid e un thread che effettua dei controlli e "dovrebbe" caricare i dati...
7
by: Olaf \El Blanco\ | last post by:
/* It look for a last name... That's work OK. If 'del' is 1, after search it will delete I want to put the field valid at NOT_VALID but even fwrite return 1, it never copy anything */ void...
3
by: nano9 | last post by:
Hola gente quisiera que alguien me pudiera ayudar con un problemilla que tengo, resulta que estoy programando en ASP con C# y estoy usando un cadbgrid que se comporta parecido a un datagrid o...
7
by: Patricia Mindanao | last post by:
I have a directory tree on my hard disc which represents all the web pages and linked stuff on my mirrored web hoster server. All web pages and files are statically linked. So dynamically...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.