.arr

public static double[,] ReadDoubleArray(string filename)
        {
            using (FileStream fstream = File.OpenRead(filename))
            {
                using (BinaryReader reader = new BinaryReader(fstream))
                {
                    // verify tags


                    char[] fileTag = { 'A', 'R', 'R', 'A', 'Y', '2', 'D', '\0' };
                    char[] typeTag = { 'D', 'O', 'U', 'B', 'L', 'E', '\0', '\0' };


                    char[] readFileTag = new char[8];
                    char[] readTypeTag = new char[8];


                    if (reader.Read(readFileTag, 0, 8) != 8 || !IsCharArrayEqual(readFileTag, fileTag))
                        throw new InvalidDataException("The file tag is invalid.");




                    if (reader.Read(readTypeTag, 0, 8) != 8 || !IsCharArrayEqual(readTypeTag, typeTag))
                        throw new InvalidDataException("The type tag is invalid.");


                    // read size
                    
                    int m = reader.ReadInt32();
                    int n = reader.ReadInt32();


                    if (m <= 0 || n <= 0)
                        throw new InvalidDataException("The array size is invalid.");


                    // read data


                    int databytes = 8 * m * n;
                    byte[] buf = new byte[databytes];
                    if (reader.Read(buf, 0, databytes) != databytes)
                        throw new InvalidDataException("Failed to read the entire data body.");


                    double[,] array = new double[m, n];
                    Buffer.BlockCopy(buf, 0, array, 0, databytes);                    


                    return array;
                }
            }
        }




        public static void WriteDoubleArray(double[,] array, string filename)
        {
            using (FileStream fstream = File.OpenWrite(filename))
            {
                using (BinaryWriter writer = new BinaryWriter(fstream))
                {
                    // write tags
                    char[] fileTag = { 'A', 'R', 'R', 'A', 'Y', '2', 'D', '\0' };
                    char[] typeTag = { 'D', 'O', 'U', 'B', 'L', 'E', '\0', '\0' };


                    writer.Write(fileTag, 0, 8);
                    writer.Write(typeTag, 0, 8);


                    // write size
                    int m = array.GetLength(0);
                    int n = array.GetLength(1);


                    writer.Write(m);
                    writer.Write(n);


                    // write data


                    int databytes = m * n * 8;
                    byte[] buf = new byte[databytes];
                    Buffer.BlockCopy(array, 0, buf, 0, databytes);


                    writer.Write(buf);
                }
            }
        }

你可能感兴趣的:(.arr)