Example #1
0
        } // writeRecord()

        // readRecord handles the details of marshaling data
        // back from the binary file into a record
        private bioStruct readRecord()
        {
            // Create a new record to hold the data read
            bioStruct rec = new bioStruct(1);

            byte[] buffer;

            // Read the data into the byte buffer buffer
            buffer = binfile.read(Marshal.SizeOf(typeof(bioStruct)));
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            // Cast the bytes into the record
            rec = (bioStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
                                                    typeof(bioStruct));
            return(rec);
        } // readRecord()
Example #2
0
        }     // struct bioStruct()

        // writeRecord translates from normal data to the
        // binary record
        public void writeRecord(int i, string s1, string s2,
                                string lat, string lon, string d, string n, string e)
        {
            // Create a new record and copy the data in, painfully
            bioStruct rec = new bioStruct(1);

            rec.id = i;
            for (int j = 0; j < s1.Length; j++)
            {
                rec.commonName[j] = s1[j];
            }
            for (int j = 0; j < s2.Length; j++)
            {
                rec.sciName[j] = s2[j];
            }
            for (int j = 0; j < lat.Length; j++)
            {
                rec.longitude[j] = lat[j];
            }
            for (int j = 0; j < lon.Length; j++)
            {
                rec.latitude[j] = lon[j];
            }
            for (int j = 0; j < d.Length; j++)
            {
                rec.date[j] = d[j];
            }
            for (int j = 0; j < n.Length; j++)
            {
                rec.name[j] = n[j];
            }
            for (int j = 0; j < e.Length; j++)
            {
                rec.age[j] = e[j];
            }

            // See the reference above for pinning and
            // marshaling details
            byte[]   buffer = new byte[Marshal.SizeOf(typeof(bioStruct))];
            GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            Marshal.StructureToPtr(rec, handle.AddrOfPinnedObject(), false);

            // Actually write the data
            binfile.write(buffer, Marshal.SizeOf(typeof(bioStruct)));
            handle.Free();
        } // writeRecord()