Search code examples
c#arraysbinaryfiles

Reading and writing two-dimensional array to file


I get a 2D array from a device at specific time intervals. I keep these series in a list. I need to write the sequences in the list to the file on the computer. After reading these series I will put it in the list again. I found a method for this. I can write the series. I have some questions at the point of reading.

When I write a fixed array; Example short[,] array2D = new short[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

I read it properly when I want to read the same series knowing its dimensions.

But when I write more than one series in a row, I want to read it piece by piece. I failed on this. How can I read the sequences from the file?

Write Code

private void testWriteArray()
    {
        for (int i = 0; i<Globals.logArrayList.Count; i++)
        {
            array2D = Globals.logArrayList[i];

            FileStream writeStream = new FileStream(filename, FileMode.Append);
            BinaryWriter writeBinary = new BinaryWriter(writeStream);
            GCHandle arrHandle = GCHandle.Alloc(array2D, GCHandleType.Pinned);
            IntPtr arrPtr = arrHandle.AddrOfPinnedObject();

            unsafe
            {
                int arrLen = array2D.Length;
                if (arrLen > 0)
                {
                    IEnumerator enumerator = array2D.GetEnumerator();
                    enumerator.MoveNext();
                    int arrSize = System.Runtime.InteropServices.Marshal.SizeOf(enumerator.Current) * arrLen;

                    using (UnmanagedMemoryStream arrStream =
                    new UnmanagedMemoryStream((byte*)arrPtr.ToPointer(), arrSize))
                    {
                        arrStream.CopyTo(writeBinary.BaseStream, (int)arrStream.Length);
                    }
                }
            }

            arrHandle.Free();

            writeStream.Flush();
            writeStream.Close();

        }
    }

Read Code

   static Array testReadArray()
    {

        int[,] array2D = new int[1000, 2000];

        FileStream readStream = new FileStream(filename, FileMode.Open);
        BinaryWriter readBinary = new BinaryWriter(readStream);
        GCHandle arrHandle = GCHandle.Alloc(array2D, GCHandleType.Pinned);
        IntPtr arrPtr = arrHandle.AddrOfPinnedObject();

        unsafe
        {
            int arrLen = array2D.Length;
            if (arrLen > 0)
            {
                IEnumerator enumerator = array2D.GetEnumerator();
                enumerator.MoveNext();
                int arrSize =
                System.Runtime.InteropServices.Marshal.SizeOf(enumerator.Current) * arrLen;

                using (UnmanagedMemoryStream arrStream = new UnmanagedMemoryStream
                ((byte*)arrPtr.ToPointer(), arrSize, arrSize, FileAccess.Write))
                {
                    readBinary.BaseStream.CopyTo(arrStream, (int)arrStream.Length);
                }
            }
        }

        arrHandle.Free();
        readStream.Close();
        return array2D;
    }

In the reading code, I don't know how many dimensions the array is in the file. That's why I gave the array size 1000,2000. Normally, although my data is between 0-255, I am reading 6-7 digit numbers while reading.

Is it possible for me to know the number of strings in the file? If so how?


Solution

  • The problem was so simple that I don't know how I missed it. The array type I defined during reading is integer, but my data is short. When the data type of the array was short the whole problem was gone.

    I also solved the total number of sequences in the file as follows.

    The lengths of the 2-dimensional array at the time of writing are 48 * 64. I divided the total number of bytes in the file by 48 * 64. Also, since my data is short (2 bytes) I divided it by 2 to get the total number of strings.

    var arrayCount = readBinary.BaseStream.Length / (48*64) / 2;