Boost logo

Boost Users :

From: Piyush Kapadia (piyush.kapadia_at_[hidden])
Date: 2005-10-23 06:05:04


I checked VC 7, header file for ofstream, destructor is empty, plus if we
put this in multithread environment, it is necessary to ensure close() is
executed before another thread attempts to access same file, unless
xml_oarchive's destructor itself has a mechanism to close the stream once
entire content is written to stream.

 

  _____

From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Robert Ramey
Sent: Saturday, October 22, 2005 8:41 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] When to close ofstream in XML serialize

 

Just remove the close(). This will permit the xml_archive destructor to be
called while the stream is still open. The xml destructor adds some final
tags to make a valid xml file.

 

Robert Ramey

"Piyush Kapadia" <piyush.kapadia_at_[hidden]> wrote in message
news:435ab0bf.7f93c92a.6d94.50a5_at_mx.gmail.com...

In following function code, ofs.close() results in failure to write last end
tag of XML archive, apparently because stream is closed before destructor is
executed, so when do we execute ofs.close() ? ofs.flush does not help either
or shell we skip executing any flush or close and just return from function
?

 

template<class T>

void Serialize(const char * filename)

{

            std::ofstream ofs(filename);

            assert(ofs.good());

            boost::archive::xml_oarchive oa(ofs);

            oa << BOOST_SERIALIZATION_NVP(T);

            ofs.close();

}

  _____

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net