#### Archived

This topic is now archived and is closed to further replies.

# Vectors and ofstream

This topic is 4981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m trying to write to a file, however the file to be written to depends on other data and I may need to write using several different ofstreams. Hence, the possible ofstreams are stored in a vector. However, when I try to use the same exact code using arrays everything is fine, but when I try to do it using the vector....it''ll open and close the file ok but just won''t write anything to it. Here''s the code....
vector <ofstream> os(0);
...
ofstream x;
os.push_back(x);
...
os[myValue].open(dav.c_str(), ios::app | ios::binary);
...
if(os[myValue].write(buffer,messageSize)){
//if(X[myValue].write(buffer, messageSize)){

cout << "written to file test" << clientSocket << endl;
}
...

Any suggestions?

##### Share on other sites
iostreams can''t be copied, you could refer to them using a vector (create the ofstreams using new). If you don''t want to have to remember to delete the pointer use a boost::shared_ptr instead (www.boost.org).

• ### Forum Statistics

• Total Topics
628647
• Total Posts
2984032

• 10
• 9
• 9
• 10
• 21