Why is this file not written to disk?

Hi guys, as homework, I need to write a program that is read from a file containing the following:

toto, M, 50

fifi, f, 60

soso, F, 70

lolo, M, 60

fifi, f, 60


and then find the following: Which character is most repeated and how many times is repeated

avgerage all students

avgerage all male

announce all women

how many are below avgerage

how many more than avgerage sign

and vice versa

how many student names start with T and end with T in the file

(all results should be placed in the output file)


I did all this without errors, but without writing it to a file, can someone tell me why and please, I do not want to use any new methods as (LINQ and other materials for promotion)

using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Exam_Ex { class Program { public static int[] ReadFile(string FileName, out string[] Name, out char[] Gender) { Name = new string[1]; int[] Mark = new int[1]; Gender = new char[1]; if (File.Exists(FileName)) { FileStream Input = new FileStream(FileName, FileMode.Open, FileAccess.Read); StreamReader SR = new StreamReader(Input); string[] Current; int Counter = 0; string Str = SR.ReadLine(); while (Str != null) { Current = Str.Split(','); Name[Counter] = Current[0]; Mark[Counter] = int.Parse(Current[2]); Gender[Counter] = char.Parse(Current[1].ToUpper()); Counter++; Array.Resize(ref Name, Counter + 1); Array.Resize(ref Mark, Counter + 1); Array.Resize(ref Gender, Counter + 1); Str = SR.ReadLine(); } } return Mark; } public static int MostFreq(int[] M, out int Frequency) { int Counter = 0; int Frequent = 0; Frequency = 0; for (int i = 0; i < M.Length; i++) { Counter = 0; for (int j = 0; j < M.Length; j++) if (M[i] == M[j]) Counter++; if (Counter > Frequency) { Frequency = Counter; Frequent = M[i]; } } return Frequent; } public static int Avg(int[] M) { int total = 0; for (int i = 0; i < M.Length; i++) total += M[i]; return total / M.Length; } public static int AvgCond(char[] G, int[] M, char S) { int total = 0; int counter = 0; for (int i = 0; i < G.Length; i++) if (G[i] == S) { total += M[i]; counter++; } return total / counter; } public static int BelowAvg(int[] M, out int AboveAvg) { int Bcounter = 0; AboveAvg = 0; for (int i = 0; i < M.Length; i++) { if (M[i] < Avg(M)) Bcounter++; else AboveAvg++; } return Bcounter; } public static int CheckNames(string[] Name, char C) { C = char.Parse(C.ToString().ToLower()); int counter = 0; string Str; for (int i = 0; i < Name.Length - 1; i++) { Str = Name[i].ToLower(); if (Str[0] == C || Str[Str.Length - 1] == C) counter++; } return counter; } public static void WriteFile(string FileName, string[] Output) { FileStream FS = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter SW = new StreamWriter(FS); for (int i = 0; i < Output.Length; i++) SW.WriteLine(Output[i]); } static void Main(string[] args) { int[] Mark; char[] Gender; string[] Name; string[] Output = new string[8]; int Frequent, Frequency, AvgAll, MaleAvg, FemaleAvg, BelowAverage, AboveAverage, NamesCheck; Mark = ReadFile("c:\\IUST1.txt", out Name, out Gender); Frequent = MostFreq(Mark, out Frequency); AvgAll = Avg(Mark); MaleAvg = AvgCond(Gender, Mark, 'M'); FemaleAvg = AvgCond(Gender, Mark, 'F'); BelowAverage = BelowAvg(Mark, out AboveAverage); NamesCheck = CheckNames(Name, 'T'); Output[0] = "Frequent Mark = " + Frequent.ToString(); Output[1] = "Frequency = " + Frequency.ToString(); Output[2] = "Average Of All = " + AvgAll.ToString(); Output[3] = "Average Of Males = " + MaleAvg.ToString(); Output[4] = "Average Of Females = " + FemaleAvg.ToString(); Output[5] = "Below Average = " + BelowAverage.ToString(); Output[6] = "Above Average = " + AboveAverage.ToString(); Output[7] = "Names With \"T\" = " + NamesCheck.ToString(); WriteFile("d:\\output.txt", Output); } } } 

Thanx in advance

+4
source share
3 answers

I have not tested it. But you should call SW.close() after the writing is complete.

+2
source

Other answers speak of calling Close explicitly - I would suggest that instead you end up using StreamWriter (and StreamReader and streams) in a using statement, for example.

 using (StreamWriter SW = new StreamWriter(FS)) { for (int i = 0; i < Output.Length; i++) { SW.WriteLine(Output[i]); } } 

This will call Dispose automatically, which closes the corresponding resource. Manually calling Close only at the end of the method is vulnerable to exceptions - usually you want to put it in a finally block, but this is what the using statement does automatically.

+8
source

In the WriteFile method after SW.WriteLine (Output [i]); execute SW.Close ();
I believe the buffer is not empty until you call Close ();

+1
source