using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace fileReader { public class fileReading { private StreamReader fileReader; // reads data from a text file public static string[] readFile(string fileName) { //Create an instance of the fileReading class. fileReading data = new fileReading(); //Data storing array string[] inputFields = new string[5000]; //Control variables int i = 0, condition = 1; bool stop = false; try { //Create a new filestream object FileStream input = new FileStream(fileName, FileMode.Open, FileAccess.Read); data.fileReader = new StreamReader(input); // Suspend the screen. } catch { //No file found exception } try { //Store each line of data into a string array for further handling while (stop != true) { // will store individual pieces of data string inputRecord = data.fileReader.ReadLine(); //End of file detector condition = String.Compare(inputRecord, "EOF", true); if (condition == 0) { stop = true; } inputFields[i] = inputRecord; //Store data i++; //increment row counter } } catch { } return inputFields; //Returns a string[] array with the data stored in it. Each row of the array represents a line in the file }//End readFile method public static int[] extractInfo(string[] dataArray) { // will store individual pieces of data string[] inputField; string[] compare = null; //Will serve as the information extractor string fileName = null; int[] numericData = new int[2]; int j = 0, restriction = 1, dimcondition = 1, optcondition = 1, namecondition = 1; bool stop = false; int dimension = 0, optimumTourLength = 0; while (stop != true) //stop condition { restriction = String.Compare(dataArray[j], "EOF", true); //Check for end of file compare = dataArray[j].Split(' '); //Get the title of each row namecondition = String.Compare(Convert.ToString(compare[0]), "NAME", true); //Check for file name dimcondition = String.Compare(Convert.ToString(compare[0]), "DIMENSION", true); //Check for city dimension try { optcondition = String.Compare(Convert.ToString(compare[2]), "Optimum", true); //Check Op Tour Length } catch { //Console.WriteLine("Not relevant"); } //End while condition if (restriction == 0) { stop = true; } //Try to retrieve file name try { if (namecondition == 0) { inputField = dataArray[j].Split(' '); fileName = inputField[2]; } } catch { } //Try to retrieve city dimension try { if (dimcondition == 0) { inputField = dataArray[j].Split(' '); dimension = Convert.ToInt32(inputField[2]); } } catch{} //Try to retrieve Optimum Tour Length try { if (optcondition == 0) { inputField = dataArray[j].Split(' '); optimumTourLength = Convert.ToInt32(inputField[6]); } } catch{} j++; //increment row counter }//End while loop numericData[0] = dimension; numericData[1] = optimumTourLength; return numericData; }//End extractInfo method public static double[,] testData(int[] dimension, string[] dataArray) { // will store individual pieces of data string[] inputField; string[] compare = null; //Will serve as the information extractor int restriction = 1, i = 0, j = 0, w = 0, tpass = 1, conversion = 0; bool stop = false; double[,] tspData = new double[dimension[0], 3]; Type t = null, t2; //Control variable for recognizing the begin of coordinate type while (stop != true) //stop condition { restriction = String.Compare(dataArray[j], "EOF", true); //Check for end of file compare = dataArray[j].Split(' '); //Get the title of each row //End while condition if (restriction == 0) { stop = true; } //Try to retrieve city coordinates try { conversion = Convert.ToInt32(compare[0]); t = conversion.GetType(); //Check for coordinate section beginning t2 = t.GetElementType(); tpass = String.Compare(Convert.ToString(t),"System.Int32", true); //Check for city dimension if (tpass == 0) { inputField = dataArray[j].Split(' '); tspData[i, w] = Convert.ToDouble(inputField[0]); w++;//Increment column counter tspData[i,w] = Convert.ToDouble(inputField[1]); w++;//Increment column counter tspData[i,w] = Convert.ToDouble(inputField[2]); i++;//Increment row counter w = 0; //Reset column counter if (i >= dimension[0]) i = 0; //reset row counter } } catch { } j++; //increment row counter }//End while loop return tspData; } }//End fileReading class }//End namespace