Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:12:46+00:00 2026-06-17T19:12:46+00:00

In the code below I try to write two objects to a file with

  • 0

In the code below I try to write two objects to a file with no extension. The problem occurs initially at the writing stage because nothing actually gets written to the file. Then the second part; the reading stage, causes an exception when the file is opened with the line:

boost::archive::text_iarchive ia(ifs);

I took this from boost example 1. I also tried following the 3rd answer for this problem on this page Boost Serialization multiple objects

    #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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:12:47+00:00Added an answer on June 17, 2026 at 7:12 pm

    Uncomment the braces surrounding the writing stage; otherwise neither the archive nor the std::ofstream are closed before trying to read.

    As mentioned by several people in the comments, the stream needs to be flushed and closed, which is done automatically when destructing your instance of std::ofstream; in that case when encountering the closing brace of the writing stage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to lock a file and write to it with the code below:
the code below gives compilation error when I try to create test t[2]; because
When writing the below my code locks up on GetResponse. Why? try { WebRequest
I try to write a simple code in GO, in which two go routine
i use below code - Just try again. - to prevent deadlock. it seems
I try use a integer array in java with the code below: public static
I have used the code below to read rfid tag values. try { if
Consider the code below. I don't understand why my GCC compiler does not try
I’m getting system error when I try to compile the code below on Visual
I get the following error when I try and execute the code down below.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.