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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:04:43+00:00 2026-05-16T06:04:43+00:00

Ok, so I currently have a binary file containing an unknown number of structs

  • 0

Ok, so I currently have a binary file containing an unknown number of structs like this:

private struct sTestStruct
{
    public int numberOne;
    public int numberTwo;
    public int[] numbers; // This is ALWAYS 128 ints long.
    public bool trueFalse;
}

So far, I use the following to read all the structs into a List<>:

List<sTestStruct> structList = new List<sTestStruct>();

while (binReader.BaseStream.Position < binReader.BaseStream.Length)
{
    sTestStruct temp = new sTestStruct();
    temp.numberOne = binReader.ReadInt32();
    temp.numberTwo = binReader.ReadInt32();
    temp.numbers = new int[128];
    for (int i = 0; i < temp.numbers.Length; i++)
    {
        temp.numbers[i] = binReader.ReadInt32();
    }
    temp.trueFalse = binReader.ReadBoolean();

    // Add to List<>
    structList.Add(temp);
}

I don’t really want to do this, as only one of the structs can be displayed to the user at once, so there is no point reading in more than one record at a time. So I thought that I could read in a specific record using something like:

fileStream.Seek(sizeof(sTestStruct) * index, SeekOrigin.Begin);

But it wont let me as it doesn’t know the size of the sTestStruct, the structure wont let me predefine the array size, so how do I go about this??

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

    The sTestStruct is not stored in one consecutive are of memory and sizeof(sTestStruct) is not directly related to the size of the records in the file. The numbers members is a reference to an array which you allocate youself in your reading code.

    But you can easily specify the record size in code since it is a constant value. This code will seek to the record at index. You can then read one record using the body of your loop.

    const Int32 RecordSize = (2 + 128)*sizeof(Int32) + sizeof(Boolean);
    fileStream.Seek(RecordSize * index, SeekOrigin.Begin); 
    

    If you have many different fixed sized records and you are afraid that manually entering the record size for each record is error prone you could devise a scheme based on reflection and custom attributes.

    Create an attribute to define the size of arrays:

    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    sealed class ArraySizeAttribute : Attribute {
    
      public ArraySizeAttribute(Int32 length) {
        Length = length;
      }
    
      public Int32 Length { get; private set; }
    
    }
    

    Use the attribute on your record type:

    private struct sTestStruct {   
      public int numberOne;   
      public int numberTwo;   
      [ArraySize(128)]
      public int[] numbers; // This is ALWAYS 128 ints long.   
      public bool trueFalse;   
    }
    

    You can then compute the size of the record using this sample code:

    Int32 GetRecordSize(Type recordType) {
      return recordType.GetFields().Select(fieldInfo => GetFieldSize(fieldInfo)).Sum();
    }
    
    Int32 GetFieldSize(FieldInfo fieldInfo) {
      if (fieldInfo.FieldType.IsArray) {
        // The size of an array is the size of the array elements multiplied by the
        // length of the array.
        var arraySizeAttribute = (ArraySizeAttribute) Attribute.GetCustomAttribute(fieldInfo, typeof(ArraySizeAttribute));
        if (arraySizeAttribute == null)
          throw new InvalidOperationException("Missing ArraySizeAttribute on array.");
        return GetTypeSize(fieldInfo.FieldType.GetElementType())*arraySizeAttribute.Length;
      }
      else
        return GetTypeSize(fieldInfo.FieldType);
    }
    
    Int32 GetTypeSize(Type type) {
      if (type == typeof(Int32))
        return 4;
      else if (type == typeof(Boolean))
        return 1;
      else
        throw new InvalidOperationException("Unexpected type.");
    }
    

    Use it like this:

    var recordSize = GetRecordSize(typeof(sTestStruct));
    fileStream.Seek(recordSize * index, SeekOrigin.Begin); 
    

    You will probably have to expand a little on this code to use it in production.

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

Sidebar

Related Questions

Say I have a binary file (generated with Java) containing a 32 bit int
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I have an Open Source app and I currently only post the binary for
Currently my program updates itself by downloading the latest .tar.gz file containing the source
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have speakers set up both in my office and in my living
I currently have an existing database and I am using the LINQtoSQL generator tool
We currently have a company email server with Exchange, and a bulk email processing
I currently have a fairly robust server-side validation system in place, but I'm looking
I currently have heavily multi-threaded server application, and I'm shopping around for a good

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.