gpt4 book ai didi

c# - 为什么这个文件实际上没有写入磁盘?

转载 作者:太空狗 更新时间:2023-10-30 00:17:45 24 4
gpt4 key购买 nike

我必须编写一个程序来读取包含以下内容的文件:

toto, M, 50
fifi, F, 60
soso, F, 70
lolo, M, 60
fifi, F, 60

然后找到以下内容:

哪个标记重复次数最多,重复了多少次?

  • 平均所有学生
  • 平均所有男性
  • 平均所有女性

有多少低于平均水平?

比平均分多多少分?

然后反转。

一个文件中有多少学生姓名以 T 开头和以 T 结尾?

(所有结果都应该放在一个out文件中)


我已经完成了所有操作,没有错误,但是它没有写在文件上,谁能告诉我为什么并且我不想使用任何新方法(LINQ 和其他先进的东西)。

    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);
}
}
}

最佳答案

其他答案讨论了显式调用 Close - 我建议您不要这样做,而是将 StreamWriter(以及 StreamReader 和流)的使用包装在 using 语句中,例如

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

这将自动调用 Dispose,这将关闭相应的资源。仅在方法末尾手动调用 Close 容易出现异常 - 您通常希望将其放在 finally block 中,但这正是 使用的 语句自动执行。

关于c# - 为什么这个文件实际上没有写入磁盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/400861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com