} // readRecord()

        // Opens a | delimited text file, reads its data in
        // normal format, calls writeRecord() to write it in
        // binary form
        public List <string[]> ProcessTextFile(string textFileName)
        {
            // Create a text file reader
            StreamReader f = new StreamReader(textFileName);

            // Create a binary file writer
            string binFileName = textFileName.Replace(".txt", ".dat");

            System.Console.WriteLine("file name = " + binFileName);
            binfile = new BinaryFile(binFileName, true);
            List <string[]> textForBox = new List <string[]>();

            // Read until done
            while (f.Peek() != -1)
            {
                // Get a line
                string str = f.ReadLine();
                // Split the line by |
                string[] s = str.Split('|');
                //For output
                textForBox.Add(s);
                // Display the data
                for (int i = 0; i < 7; i++)
                {
                    System.Console.WriteLine(s[i]);
                } // for

                // Write to the binary file
                writeRecord(Convert.ToInt32(s[0]), s[1], s[2], Convert.ToDouble(s[3]), Convert.ToDouble(s[4]), s[5], s[6], Convert.ToInt32(s[7]));
            } // while

            // Close the text and binary output files
            f.Close();
            binfile.Close();
            return(textForBox);
        } // ProcessTextFile()
예제 #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()