Vector of ofstream
Hi everyone,
I was wondering that can I use something like in C++ :
vector < ofstream > fileVect;
and store file pointers in it.
I know in C , we can do something like that FILE *tfile[5];
Regards
Tapajyoti
[266 byte] By [
tdas] at [2007-11-11 8:38:42]

# 1 Re: Vector of ofstream
Hi,
I've tried to use a vector of ofstreams but run into problems.
ofstream file;
vector<ofstream> fileVect;
fileVect.resize(5);
file.open("c:\\text0.txt",ios::out|ios::binary);
fileVect[0].open("c:\\text1.txt",ios::out|ios::binary);
fileVect[1].open("c:\\text2.txt",ios::out|ios::binary);
file << "This is file text0.txt" << endl;
fileVect[0] << "This is file text1.txt" << endl;
fileVect[1] << "This is file text2.txt" << endl;
file.close();
fileVect[0].close();
fileVect[1].close();
The file c:\text0.txt has a line of text in it as expected, but c:\text1.txt and c:\text2.txt are both empty, which is quite distressing. It might be one for Danny...
But then I tried that on VS6 where the STL is quite out of date.
Dieter
# 2 Re: Vector of ofstream
Hi drky,
As I was using g++ as the compiler, so I used the C type array of FILE *tmpfile[5] to store the filepointers.I could not find any other solution, although I am sure there would be something more sophisticated.
Regards
Tapajyoti
tdas at 2007-11-11 21:02:09 >

# 3 Re: Vector of ofstream
Hi,
I've tried to use a vector of ofstreams but run into problems.
The file c:\text0.txt has a line of text in it as expected, but c:\text1.txt and c:\text2.txt are both empty, which is quite distressing. It might be one for Danny...
Dieter
Sure is...
The problem is with the resize call. It doesn't do exactly what you expect it to. Without getting into gory details, you need to create a vector of 5 empty ofstream objects like this:
ofstream file;
vector<ofstream> fileVect (5); //vector contains 5 ofstream objects
file.open("c:\\text0.txt",ios:ut|ios::binary);
fileVect[0].open("c:\\text1.txt",ios:ut|ios::binary);
fileVect[1].open("c:\\text2.txt",ios:ut|ios::binary);
file << "This is file text0.txt" << endl;
fileVect[0] << "This is file text1.txt" << endl;
fileVect[1] << "This is file text2.txt" << endl;
file.close();
Danny at 2007-11-11 21:03:12 >

# 4 Re: Vector of ofstream
Thanks Danny,
but still no good on my compiler - same result.
I can't think what the "gory details" would be. Can you post a link?
The only thing I could think of is, that the default constructor for
ofstream is not called on a vector<...>::resize, but that surely would
create all sorts of problems all over the shop.
Cheers,
D
# 5 Re: Vector of ofstream
If I am not mistaken, what I found out is ofstream does not have a copy constructor and hence is can not be used like that in a vector. PLease correct me if I am wrong
Tapajyoti
tdas at 2007-11-11 21:05:17 >

# 6 Re: Vector of ofstream
I thibk I finally hit it: ofstream cannot be a member of STL containers because it's not copyable. You need to use instead a vector of pointers to ofstream and populate it with pointers to live objects:
vector <ofstream *> vpf;
vpf.push_back (new ofstream("myfile1.txt"));
...
vpf[0]->close();
Danny at 2007-11-11 21:06:17 >

# 7 Re: Vector of ofstream
I thought that vectors were really made to work with objects and not pointers?
rssmps at 2007-11-11 21:07:21 >

# 8 Re: Vector of ofstream
they were meant to work with objects that are assignable and copy cnstructible, which covers 99% of the objects I know. But file stream objects are indeed a special case. How can you copy a file state? Should the two object share the same file? read/write simultanesouly from it? This would be disastrous. So pointers offer the extra level of indirection.
Danny at 2007-11-11 21:08:14 >
