} // ProcessTextFile()

        // ProcessBinaryFile reads records from an existing
        // binary file and displays them
        public void ProcessBinaryFile(string binaryFileName)
        {
            string txtFileName = binaryFileName.Replace(".dat", "_2.txt");

            bioStruct rec;  // one record

            long where = 0; // file byte counter
            int i = 0;      // file record counter

            // Open the binary file for read access
            binfile = new BinaryFile(binaryFileName, false);
            // Get its length
            long length = binfile.getLength();

            TextWriter writer = new StreamWriter(txtFileName);

            // Read the first record
            rec = readRecord();
            System.Console.WriteLine("Read from binary file");

            // Continue while not at end-of-file
            while (where < length)
            {
                // Convert the record to regular data and
                // display it
                System.Console.WriteLine("Record # " + i);
                System.Console.WriteLine("id: " + rec.id);
                writer.Write(rec.id.ToString().Trim() + "|");
                string str = new string(rec.commonName);
                System.Console.WriteLine("common name: " + str);
                writer.Write(str.ToString().Trim() + "|");
                string str2 = new string(rec.sciName);
                System.Console.WriteLine("scientific name: " + str2);
                writer.Write(str2.ToString().Trim() + "|");
                System.Console.WriteLine("latitude: " + rec.latitude);
                writer.Write(rec.latitude.ToString().Trim() + "|");
                System.Console.WriteLine("longitude: " + rec.longitude);
                writer.Write(rec.longitude.ToString().Trim() + "|");
                string str4 = new string(rec.date);
                System.Console.WriteLine("date: " + str4);
                writer.Write(str4.ToString().Trim() + "|");
                string str5 = new string(rec.name);
                System.Console.WriteLine("name: " + str5);
                writer.Write(str5.ToString().Trim() + "|");
                System.Console.WriteLine("num: " + rec.number);
                writer.Write(rec.number.ToString().Trim() + "|");
                writer.Write("\n");

                // Read the next record
                rec = readRecord();

                // Update the byte and record counters
                where += Marshal.SizeOf(typeof(bioStruct));
                i++;
            } // while

            // Close the binary file
            binfile.Close();
            writer.Close();
        } // ProcessBinaryFile()
Esempio n. 2
0
        } // ProcessTextFile()

        // ProcessBinaryFile reads records from an existing
        // binary file and displays them
        public void ProcessBinaryFile(string binaryFileName)
        {
            bioStruct rec;  // one record

            long where = 0; // file byte counter
            int i = 0;      // file record counter

            // Open the binary file for read access
            binfile = new BinaryFile(binaryFileName, false);
            // Get its length
            long length = binfile.getLength();

            // Read the first record
            rec = readRecord();
            System.Console.WriteLine("Read from binary file");

            string path = @"C:\Users\ryant\Documents\GitHub\CSCI-3230-Algorithms\Files\";

            using (StreamWriter outputFile = new StreamWriter(Path.Combine(path, "test_2.txt")))
            {
                // Continue while not at end-of-file
                while (where < length)
                {
                    // Convert the record to regular data and
                    // display it
                    System.Console.WriteLine("Record # " + i);
                    System.Console.WriteLine("id: " + rec.id);
                    outputFile.Write(rec.id + "|");
                    string str = new string(rec.commonName);
                    System.Console.WriteLine("common name: " + str);
                    string output = "";
                    for (int z = 0; z < rec.commonName.Length; z++)
                    {
                        if (rec.commonName[z] == ' ' && rec.commonName[z + 1] == ' ')
                        {
                            break;
                        }
                        output += rec.commonName[z];
                    }
                    outputFile.Write(output + "|");
                    output = "";
                    string str2 = new string(rec.sciName);
                    System.Console.WriteLine("scientific name: " + str2);

                    for (int z = 0; z < rec.sciName.Length; z++)
                    {
                        if (rec.sciName[z] == ' ' && rec.sciName[z + 1] == ' ')
                        {
                            break;
                        }
                        output += rec.sciName[z];
                    }
                    outputFile.Write(output + "|");
                    output = "";
                    string strlat = new string(rec.latitude);
                    System.Console.WriteLine("latitude: " + strlat);
                    for (int z = 0; z < rec.longitude.Length; z++)
                    {
                        if (rec.longitude[z] == ' ' && rec.longitude[z + 1] == ' ')
                        {
                            break;
                        }
                        output += rec.longitude[z];
                    }
                    outputFile.Write(output + "|");
                    output = "";
                    string strlong = new string(rec.longitude);
                    System.Console.WriteLine("longitude: " + strlong);
                    for (int z = 0; z < rec.latitude.Length; z++)
                    {
                        if (rec.latitude[z] == ' ' && rec.latitude[z + 1] == ' ')
                        {
                            break;
                        }
                        output += rec.latitude[z];
                    }
                    outputFile.Write(output + "|");
                    output = "";
                    string str4 = new string(rec.date);
                    System.Console.WriteLine("date: " + str4);
                    for (int z = 0; z < rec.date.Length; z++)
                    {
                        if (rec.date[z] == ' ' && rec.date[z + 1] == ' ')
                        {
                            break;
                        }
                        output += rec.date[z];
                    }
                    outputFile.Write(output + "|");
                    output = "";
                    string str5 = new string(rec.date);
                    System.Console.WriteLine("name: " + str5);
                    for (int z = 0; z < rec.name.Length; z++)
                    {
                        if (rec.name[z] == ' ' && rec.name[z + 1] == ' ')
                        {
                            break;
                        }
                        output += rec.name[z];
                    }
                    outputFile.Write(output + "|");
                    output = "";
                    string str6 = new string(rec.age);
                    System.Console.WriteLine("age: " + str6);
                    for (int z = 0; z < rec.age.Length; z++)
                    {
                        if (rec.age[z] == ' ' && rec.age[z + 1] == ' ')
                        {
                            break;
                        }
                        output += rec.age[z];
                    }
                    outputFile.Write(output + "|");
                    output = "";
                    outputFile.Write("\n");

                    // Read the next record
                    rec = readRecord();

                    // Update the byte and record counters
                    where += Marshal.SizeOf(typeof(bioStruct));
                    i++;
                } // while
            }
            // Close the binary file
            binfile.Close();
        } // ProcessBinaryFile()