When to close ofstream in XML serialize

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(); }

You don't need the ofs.close() call. The destructor of std::ofstream will take care of this. What is T in your code? I think you need to pass an object in the BOOST_SERIALIZATION_NVP macro. Take a look at the xml example in demo_xml_save.cpp Christian On 10/22/05, Piyush Kapadia <piyush.kapadia@gmail.com> wrote:
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

T is a template class name(generic) -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Christian Henning Sent: Saturday, October 22, 2005 6:56 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] When to close ofstream in XML serialize You don't need the ofs.close() call. The destructor of std::ofstream will take care of this. What is T in your code? I think you need to pass an object in the BOOST_SERIALIZATION_NVP macro. Take a look at the xml example in demo_xml_save.cpp Christian On 10/22/05, Piyush Kapadia <piyush.kapadia@gmail.com> wrote:
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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@gmail.com> wrote in message news:435ab0bf.7f93c92a.6d94.50a5@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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Saturday, October 22, 2005 8:41 PM To: boost-users@lists.boost.org 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@gmail.com> wrote in message news:435ab0bf.7f93c92a.6d94.50a5@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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hmm - you might double check. It would be the stream buffer destructor rather than the ofstream itself. Check the example with the package "demo_xml_save.cpp" This example works. Robert Ramey "Piyush Kapadia" <piyush.kapadia@gmail.com> wrote in message news:435b6e66.02244cbd.6009.ffffbf85@mx.gmail.com... 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@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Saturday, October 22, 2005 8:41 PM To: boost-users@lists.boost.org 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@gmail.com> wrote in message news:435ab0bf.7f93c92a.6d94.50a5@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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Christian Henning
-
Piyush Kapadia
-
Robert Ramey