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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:50:15+00:00 2026-05-14T06:50:15+00:00

I have to write an object in to binary file.My struct looks like this.

  • 0

I have to write an object in to binary file.My struct looks like this.

   Struct Company
    {
       int numberofemployees
       list of Struct Employee.
    }

    Struct Employee
    {
       string EmployeeName;
       string Designation;
    }

What is the best way to do the above operation?
Regards
Raju

  • 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-14T06:50:16+00:00Added an answer on May 14, 2026 at 6:50 am

    What exactly do you want the output to look like? You can write it manually (see Lirik’s answer), or if you want runtime support, perhaps something like protobuf-net.

    This would be trivial to do if you were using classes (which I expect you actually should be), but additionally protobuf-net v2 (only available as source at the moment) should work with that “as is”.

    For info, here is how I would do it as classes:

        public class Company
        {
            private readonly List<Employee> employees = new List<Employee>();
            public List<Employee> Employees { get { return employees;}}
        }
    
        public class Employee
        {
            public string EmployeeName {get;set;}
            public string Designation {get;set;}
        }
    

    This could be decorated with serialization attributes, or (again, using protobuf-net v2) something like this test (which passes):

        [Test]
        public void CanSerializeCompany()
        {
            var model = TypeModel.Create();
            model.Add(typeof(Company), false).Add("Employees");
            model.Add(typeof(Employee), false).Add("EmployeeName", "Designation");
            model.CompileInPlace();
    
            Company comp = new Company {
                Employees = {
                    new Employee { Designation = "Boss", EmployeeName = "Fred"},
                    new Employee { Designation = "Grunt", EmployeeName = "Jo"},
                    new Employee { Designation = "Scapegoat", EmployeeName = "Alex"}}
            }, clone;
            using(var ms = new MemoryStream()) {
                model.Serialize(ms, comp);
                ms.Position = 0;
                Console.WriteLine("Bytes: " + ms.Length);
                clone = (Company) model.Deserialize(ms, null, typeof(Company));
            }
            Assert.AreEqual(3, clone.Employees.Count);
            Assert.AreEqual("Boss", clone.Employees[0].Designation);
            Assert.AreEqual("Alex", clone.Employees[2].EmployeeName);
        }
    

    (and writes 46 bytes)

    It should work with private fields, structs, etc – I’d have to take a look…

    If you are able to add attributes, then you don’t need to set up the model manually (the first 4 lines). The rest of the code is just showing full round-trip usage.

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

Sidebar

Related Questions

I have a problem tring to write to a binary file. //This is preparing
I have a binary string that I need to write to a file. I
I have a an object I'd like to be able to read and write
I'm trying to build an object that looks something like this: public class MyObject
I have a C++ objects that looks like this class myClass { vector<OtherClass*> otherClassVector;
I have an object with several text strings as members. I want to write
I have a specific object with image data. And I want read/write images (in
I have an link in HTML and I use Page Object pattern to write
I have write this code but it does work only if I am already
I am using MSSQL 2005. I have StringIO object that contains my zip file

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.