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 8299497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:10:33+00:00 2026-06-08T16:10:33+00:00

So I make a serialization of a single object but I had problem with

  • 0

So I make a serialization of a single object but I had problem with several.
Here is the code:

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    class MyTest
    {
    private:
        string test;
    public:
        MyTest():test(""){};
        void setTest(const string& test) {this->test = test;};
        string getTest() const {return this->test;};
    };
    void writeToFile(const MyTest& m)
    {
        ofstream ofs("data.mbp", ios::app|ios::binary);
        ofs.clear();
        ofs.write((char *)&m, sizeof(m));
        ofs.flush();
        ofs.close();
        return;
    };
    MyTest& readTest(MyTest& m,int num)
    {
        ifstream ifs;
        ifs.open("data.mbp", ios::in|ios::binary);
        for ( int i = 1 ; i <= num ; i++)
            ifs.read((char *)&m, sizeof(m));
        return m;
    }

    int main(int argc,char* argv[])
    {
        MyTest m,t;
        m.setTest("Hello");
        writeToFile(m);
        t.setTest("World");
        writeToFile(t);
        t = readTest(t,1);
        cout << t.getTest() << endl;

        m = readTest(m,2);
        cout << m.getTest() << endl;


        return 0;
    }

The problem is that I do not know how to write two or more objects in a binary file and after that how can I read them.
Does anybody know?

Thanks in advance.

  • 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-08T16:10:35+00:00Added an answer on June 8, 2026 at 4:10 pm

    I recommend you using Boost – Serialization for serializing objects in C++: http://www.boost.org/libs/serialization/

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

Sidebar

Related Questions

I am currently using Serialization in order to make deep copies of some autogenerated
I've to make a code to upload/download a file on remote machine. But when
I'm using jQuery.ajax() to make a PUT request to a REST web service, but
I previously asked this question about how to make versioned serializaion using boost::serialization::access I
I am using boost serialization on windows, and I wanted to test my code
I am planning to use serialization to do clone. I have to make my
make /bin/sh /Users/myusername/Downloads/php-5.3.5/ext/mssql/libtool --mode=compile cc -I. -I/Users/myusername/Downloads/php-5.3.5/ext/mssql -DPHP_ATOM_INC -I/Users/myusername/Downloads/php-5.3.5/ext/mssql/include -I/Users/myusername/Downloads/php-5.3.5/ext/mssql/main -I/Users/myusername/Downloads/php-5.3.5/ext/mssql -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM
I am planning to try to use XML Serialization in C# but I am
I'm using SWIG to make a Java wrapper of a C++ library (about Json
I'd like to make a connection to Geoserver to edit several point of a

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.