} // readRecord() // Opens a | delimited text file, reads its data in // normal format, calls writeRecord() to write it in // binary form public void 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); // Read until done while (f.Peek() != -1) { // Get a line string str = f.ReadLine(); // Split the line by | string[] s = str.Split('|'); // Display the data for (int i = 0; i < 8; i++) { System.Console.WriteLine(s[i]); } // for // Write to the binary file writeRecord(Convert.ToInt32(s[0]), s[1], s[2], (s[3]), (s[4]), s[5], s[6], s[7]); } // while // Close the text and binary output files f.Close(); binfile.Close(); } // ProcessTextFile()
} // 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()
} // 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()