Sunday, September 29, 2013

Managing files

In developing the calculate execution time function, I had to write the intermediate results(execution times of different algorithms with different data sets) into separate text files using commands in a batch file I created. These result file were created in "C:\AlgorithmAnalysisTool\Results" file.

Later I had to read these text files and get the execution times and create a tabular graph to present the results clearly as the final output of this function.

In reading the intermediate results from these text files, the order of readings was important because I put it to an array and later I write to the graph with the same order.

foreach (string fileName in Directory.GetFiles(@"C:\AlgorithmAnalysisTool\Results"))
{
         numOfFilesR++;
 }

  string[] time = new string[numOfFilesR];
  int timeIndex = 0;

 // get your files (names)
   string[] fileNames = Directory.GetFiles(@"C:\AlgorithmAnalysisTool\Results");

This Directory.GetFiles() does not read the files in the order of occurrence. That was the problem for me. So I sorted the files according to the creation time.

// Now read the creation time for each file
      DateTime[] creationTimes = new DateTime[fileNames.Length];
      for (int i = 0; i < fileNames.Length; i++)
             creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;

// sort it
    Array.Sort(creationTimes, fileNames);

    foreach (string file in fileNames)
    {
               StreamReader fileRes = new StreamReader(file);
               time[timeIndex] = fileRes.ReadLine();                       //taking the execution times
               fileRes.Close();
               timeIndex++;
     }

After sorting the file names from the creation times I read the execution times which is inside these files and put it to the array 'time[]'.

So,  later I take the execution times from the time[] and write to a text file called "FinalResult" as a tabular graph.

No comments:

Post a Comment