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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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).

Share this post


Link to post
Share on other sites