private static float GenerateMemoryFileGrades(string path) { int totalMemoryStrings = MemoryFile.CountMemoryStrings(path); string bareFilename = Path.GetFileNameWithoutExtension(Path.GetFileName(path)); string gradesfilepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), bareFilename + ".grades"); using (TextWriter textWriter = new StreamWriter(gradesfilepath)) { for (int i = 1; i <= totalMemoryStrings; i++) { textWriter.WriteLine(String.Format("{0} {1}", i, 0.0f)); } //Close handle to MemoryString Grades file textWriter.Close(); } return(0.0f); }
private static float CalcAvgGradeFromMemoryFile(string path) { float avgGrade = 0.0f; int totalMemoryStrings = MemoryFile.CountMemoryStrings(path); //Read grades if counts!= then append grades with 0.0 int totalGrades = 0; string bareFilename = Path.GetFileNameWithoutExtension(Path.GetFileName(path)); string gradesfilepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), bareFilename + ".grades"); using (TextReader textReader = new StreamReader(gradesfilepath)) { string line = null; while (null != (line = textReader.ReadLine())) { switch (line) { case "": //Eat empty lines break; default: string[] brokenLine = line.Split(' '); if (2 != brokenLine.Length) { throw new MemoryException(String.Format("[{0}] MemoryStrings Grade File paring error", gradesfilepath)); } int memoryStringNo; if (false == int.TryParse(brokenLine[0], out memoryStringNo)) { throw new MemoryException(String.Format("[{0}] MemoryStrings Grade File paring error", gradesfilepath)); } if (memoryStringNo != totalGrades + 1) { //There must be a strinct sequence in grades throw new MemoryException(String.Format("[{0}] MemoryStrings Grade File paring error", gradesfilepath)); } float memoryStringGrade; if (false == float.TryParse(brokenLine[1], out memoryStringGrade)) { throw new MemoryException(String.Format("[{0}] MemoryStrings Grade File paring error", gradesfilepath)); } avgGrade += memoryStringGrade; totalGrades++; break; } } //Close handle to MemoryString Grades file textReader.Close(); } if (totalGrades != totalMemoryStrings) { //Append 0.0 grades for new MemoryStrings using (TextWriter textWriter = new StreamWriter(gradesfilepath, true)) { for (int i = totalGrades + 1; i <= totalMemoryStrings; i++) { textWriter.WriteLine(String.Format("{0} {1}", i, 0.0f)); } //Close handle to MemoryString Grades file textWriter.Close(); } } return(avgGrade / totalMemoryStrings); }