Reading a Binary file in Random Access Mode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Boccabell
    New Member
    • Mar 2011
    • 3

    Reading a Binary file in Random Access Mode

    Hi.
    Ok - I'm a VB6 programmer from way back and I am starting to use C# for a particular project. To add spice to the issue this is the MicroFramework because the end application will be running on a FEZ Mini.

    Now - I have seen many articles where one can write a fixed length record file from VB6 and read it in with C#. But for this project it is not going to be a sequential read, I will need to jump from record to record. Fortunately I will only be reading the file.

    If I look at the disk file as an array of structures each 14 bytes long then I should beable to jump forward and back to the appropriate record. But in C# array's start at position 0.

    Would I be correct in assuming that the byte pointer for the file also starts at 0, or doe it start at 1.

    Knowing this I can then write the code to jump forward or back through the file knoing that I will always arrive at the start of a 'record'

    Many thanks
    Dave
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #2
    If I understand your question correctly, then yes, byte arrays have a 0-based index. Example:

    Code:
    byte[] arr = new byte[] { 1, 2, 3, 4 };
    MessageBox.Show(arr[0].ToString());
    That would show a message box with 1 in it, since 1 is the 0th (first) item in the byte array.

    Comment

    • David Boccabell
      New Member
      • Mar 2011
      • 3

      #3
      Hi HaL02FrEeK
      I think I can explain better. I have a binary file that if read in it's entirity would be

      123451234512345 1234512345

      Now I want to use this as a random file so I can read in 5 bytes at a time. Knowing that the record length is 5 bytes in length I could say

      Start of 3rd record = recordlength (5) * (Recs -1) (2) = 10
      Byte Postion 10 + 1 = Start of Rec# 3 which would be BytePosition 11

      So I can then Seek(11, SeekOrigin.Begi n)

      And I can then do a ReadBytes(5) to bring that record in for processing.

      I just need to know if my calculations and byte positioning is correct to get me to the start of the required record.

      Many thanks
      Below is a piece of code that I will start as a basis for working on.

      Code:
      class Program
      {
          static void Main()
          {
              // 1.
              // Open file with a BinaryReader.
              using (BinaryReader b = new BinaryReader(File.Open("perls.bin", FileMode.Open)))
              {
                  // 2.
                  // Variables for our position.
                  int pos = 50000;
                  int required = 2000;
      
                  // 3.
                  // Seek to our required position.
                  b.BaseStream.Seek(pos, SeekOrigin.Begin);
      
                  // 4.
                  // Read the next 2000 bytes.
                  byte[] by = b.ReadBytes(required);
              }
          }
      }

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        Counting starts at 0, so to use your simple example, let's break up the bytes into groups:

        12345 12345 12345

        You're going to be reading this as 3 arrays with 5 bytes each. The first array is 0, the second is 1. Use this for your math, along with the length of 5:

        array_length: 5
        array_index: 2 (the third array)

        Seek = array_length * array_index
        Seek = 10

        Meaning that byte 10 will be the first byte of the third array. All counting starts at 0, so if you want to get at the third grouping of bytes, your index is 2. If you want the first group of bytes, your index is 0. Multiply the index by the length of the arrays.

        I'm rambling. Does this make sense?

        Comment

        • David Boccabell
          New Member
          • Mar 2011
          • 3

          #5
          Yes it does make sense and it is what I was looking for. Trying to find out what arrays/files start at can be an issue. With some things it is 1 with other it is 0... Which can totally throw off one's caculations.

          Many thanks for your help
          Dave

          Comment

          Working...