C++ Boost序列化IO错误

C++ Boost序列化IO错误,c++,serialization,boost,C++,Serialization,Boost,在下面的代码中,我尝试将两个对象写入一个没有扩展名的文件。问题最初出现在写入阶段,因为实际上没有写入文件。第二部分;在读取阶段,使用以下行打开文件时会导致异常: boost::archive::text\u iarchive ia(ifs) 我从boost示例1中获取了这个。我还试着在这一页上找到这个问题的第三个答案 #包括 //包括以简单文本格式实现存档的标题 #包括 #包括 ///////////////////////////////////////////////////////////

在下面的代码中,我尝试将两个对象写入一个没有扩展名的文件。问题最初出现在写入阶段,因为实际上没有写入文件。第二部分;在读取阶段,使用以下行打开文件时会导致异常:

boost::archive::text\u iarchive ia(ifs)

我从boost示例1中获取了这个。我还试着在这一页上找到这个问题的第三个答案

#包括
//包括以简单文本格式实现存档的标题
#包括
#包括
/////////////////////////////////////////////////////////////
//gps坐标
//
//演示了简单类型的序列化
//
类gps_位置
{
私人:
好友类boost::serialization::access;
//当类归档对应于输出归档时
//运算符的定义类似于。
模板
无效序列化(存档和ar,常量未签名整数版本)
{
ar&学位;
应收账款&分钟;
ar&秒;
}
国际学位;
整数分钟;
浮动秒;
公众:
gps_位置(){};
gps_位置(整数d、整数m、浮点s):
度(d)、分(m)、秒(s)
{}
};
int main()
{
//创建并打开用于输出的角色存档
std::ofs流(“文件名”,std::ios::app);
//创建类实例
全球定位系统位置g0(35,59,24.567f);
gps_位置g1(35,59,88.567f);
//将数据保存到存档
//{
boost::archive::text\u oarchive oa(ofs);
//将类实例写入存档
大小\u t对象的数量\u=2;
oa>newg0;
ia>>新G1;
//调用析构函数时归档和流关闭
//}
返回0;
}

取消对写作阶段周围大括号的注释;否则,在尝试读取之前,归档文件和std::ofstream都不会关闭


正如一些人在评论中提到的,流需要刷新和关闭,这是在销毁您的
std::ofstream
实例时自动完成的;在这种情况下,当遇到编写阶段的右括号时。

我不确定,但可能需要将
std::ios::app
std::ios::out
结合起来,才能真正写入文件。这是否类似于std::ofs流(“文件名”,std::ios::app | std::ios::out)?不,我试过了,它崩溃了!是:
std::ios::app | std::ios::out
刷新并关闭流,然后尝试重新取消对写阶段周围大括号的注释;否则,在尝试读取之前,归档和ofstream都不会关闭。
    #include <fstream>

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
/////////////////////////////////////////////////////////////
// gps coordinate
//
// illustrates serialization for a simple type
//
class gps_position
{
    private:
        friend class boost::serialization::access;
    // When the class Archive corresponds to an output archive, the
    // & operator is defined similar to <<.  Likewise, when the class Archive
    // is a type of input archive the & operator is defined similar to >>.
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & degrees;
            ar & minutes;
            ar & seconds;
        }
        int degrees;
        int minutes;
        float seconds;
    public:
        gps_position(){};
        gps_position(int d, int m, float s) :
        degrees(d), minutes(m), seconds(s)
    {}
};
int main()
{
    // create and open a character archive for output
    std::ofstream ofs("filename",std::ios::app);

    // create class instance

    gps_position g0(35, 59, 24.567f);
    gps_position g1(35, 59, 88.567f);
    // save data to archive
    //{
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        size_t number_of_objects = 2;
        oa << number_of_objects;
        oa << g0;
        oa << g1;
        // archive and stream closed when destructors are called
   // }

    // ... some time later restore the class instance to its orginal state

    gps_position newg0;
    gps_position newg1;
   // {
        // create and open an archive for input
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive


        ia >> number_of_objects;
        ia >> newg0;
        ia >> newg1;
        // archive and stream closed when destructors are called
    //}

    return 0;
}