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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:08:14+00:00 2026-05-17T17:08:14+00:00

I want serialize object to binary file using operator <<, but when I serialize,

  • 0

I want serialize object to binary file using operator “<<“, but
when I serialize, for example, int fields, I obtained it’s symbolic representation:

ofstream out("file", ios::out | ios::binary);
int i=0xAA;
out << i;

And output:

0x31 0x37 0x30

i.e. (0xAA -> 170)

170

If I use write function, all ok:

out.write((char*)&i,sizeof(int));

Output:

0xAA 0x00 0x00 0x00

But can I use << instead write function, to serialize object?
Like:

out << obj.field1 << obj.field2; // etc.
  • 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-05-17T17:08:15+00:00Added an answer on May 17, 2026 at 5:08 pm

    First, a warning: You do know that the bytes within an int, or anything similar, depends on your compiler, computer, and operating system, right? Other systems might output bytes 0x00 0x00 0x00 0xAA for your example above, or something else entirely. Which means that if you send those bytes to a different computer and attempt to read them, you won’t necessarily get your original int back.

    Anyway. If you just want to spit out entire objects, one way to set this up would be to just define serialization for your class(es) and/or struct(s) by overloading operator<<:

    std::ostream& operator<<(std::ostream& out, const MyClass& obj) {
      out.write(reinterpret_cast<const char*>(&obj.field1),
                sizeof(obj.field1)); // or something better defined
      // ...
      return out;
    }
    
    ofstream out("file", ios::out | ios::binary);
    out << obj1 << obj2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Telerik controls in my webforms and want to serialize object on
I want to serialize my enum-value as an int, but i only get the
I want to make a binary serialize of an object and the result to
I want to set some attributes just before the object is serialized, but as
I want to serialize objects to text, but I want to preserve the type
I have a data object that is deep-cloned using a binary serialization. This data
I'm currently convering my ASP.NET v2 application to serialize/deserialize it's objects because I want
I have a collection of classes that I want to serialize out to an
in a C program I have an long* that I want to serialize (thus
How to save class object to file and then encrypt it with open and

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.