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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:15:21+00:00 2026-05-28T11:15:21+00:00

Note: I’m aware about the disadvantages of using BinaryFormatter in large files. But this

  • 0

Note: I’m aware about the disadvantages of using BinaryFormatter in large files. But this is a homework for my friend:

(.Net Framework 4)I have created a simple Person class which should be serialized and written in a binary file.

  • For insertion, here’s how it goes: Open/Create File, Seek end of the file, Add a serialized Person object, close the file. I don’t want to serialize a List<Person> and write it all at once.
  • For Reading (based on the index parameter): Open the file, seek the index position (based on object length), deserialize the object at index, return the object.
  • Deletion: just like reading the record at index position, go to index position, delete it, save the file.

I’m not sure if using the BinaryFormatter is the right way to do this. e.g for insert, I’ve seen examples like:

FileStream fs = new FileStream(_fileName, FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, person);
fs.Close();  

which causes all the data in file to be lost and the file will only contain the person object. How can I do the Binary read/write/delete of objects in a sequential way in a file? My guess for insertion was to use another stream, serialize the object into that stream. write the stream to a byte array and use that byte array to write to the end of my main file stream. But I couldn’t think of a suitable way for insert/delete operations. Any better suited approach to use instead of BinaryFormatter?

Note: To be more clear, he told me the teacher wants them to calculate the amount of time for each operation.
Thank you.

  • 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-28T11:15:22+00:00Added an answer on May 28, 2026 at 11:15 am

    Your code is correct except for the first line:

    FileStream fs = new FileStream(_fileName, FileMode.Append)
    

    That will allow the formatter.Serialize(fs,person) to append itself to the file.

    var listFromFile = new List<Person>();    
    
    using (var fileStream = new FileStream("C:\file.dat", FileMode.Open))
    {
        var bFormatter = new BinaryFormatter();
        while (fileStream.Position != fileStream.Length)
        {
             list.Add((Person)bFormatter.Deserialize(fileStream));
        }
    }
    

    That should work to get the list from the file, as far as insertion goes…you may have to rewrite the objects after you read them with the new object inserted, same with deletion.

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

Sidebar

Related Questions

(Note: I thought about posting this to serverfault, but I figured more developers have
Note, this might perhaps be best suited on serverfault.com, but since it is about
Note: This question uses jQuery but the question has nothing to do with jQuery
Note the tag: VBA , not VB6, not VB.NET. This is specific to VBA
NOTE: The scenario is using 2 entity framework models to sync data between 2
note: I'm working on wordpress but this isn't a wordpress question. I have a
note: question title is change as discussed in this meta Q&A I'm using the
Note: This might seem like a Super User question at first, but please read
Note bene: I realize this is an immensely complicated question with about a million
Note: I am not sure if this is better here, or at superuser, but

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.