我如何重写这个输入/输出文件,使我的输入文件是一个逗号分隔的文件



我如何重写这个输入/输出文件,使我的输入文件是一个逗号分隔的文件(input.csv)?

输入文件(Input .csv):
Smith,John,87
Doe,Jane,93
Sacramanto,Bob,57

我是初学者,老师说我们必须使用line.indexOf()函数。

// constant variables
const string INPUT_FILE = "input.txt";
const string OUTPUT_FILE = "output.txt";
// open the input file
StreamReader sr = new StreamReader(INPUT_FILE);
// fields used for input file
string? line = "";
string firstName = "", lastName = "";
double mark = 0;
// variables for calculating average
double total = 0, count = 0, avg = 0;
// read the first line of text from the input file
line = sr.ReadLine();

// continue to read until you reach end of file

while (line != null)
{
string [] values = line.Split(',');
// get firstName
firstName = line;
// read next line & get last name
line = sr.ReadLine();
lastName = line;
// read next line & get mark
line = sr.ReadLine();
mark = Convert.ToDouble(line);
Console.WriteLine(firstName + ' ' + lastName + ": " + mark);
// accumulate 'total' & increment 'count'
total = total + mark;
count++;
// read the next line
line = sr.ReadLine();
}
//close input file
sr.Close();

avg = total / count;
Console.WriteLine("nClass Average: " + avg);
// open an output file
StreamWriter sw = new StreamWriter(OUTPUT_FILE);
sw.WriteLine(avg);
sw.Close();

我想你把linevalues搞混了。line为实际线,values为单线的分隔值。每次循环迭代都必须读取一行。试试下面的代码

// constant variables
const string INPUT_FILE = "input.txt";
const string OUTPUT_FILE = "output.txt";
// open the input file
StreamReader sr = new StreamReader(INPUT_FILE);
// fields used for input file
string? line = "";
string firstName = "", lastName = "";
double mark = 0;
// variables for calculating average
double total = 0, count = 0, avg = 0;
// continue to read until you reach end of file

while (!sr.EndOfStream)
{
// read the first line of text from the input file
line = sr.ReadLine();
if (line != null)
{
string[] values = line.Split(',');
if (values.Count() == 3)
{
firstName = values[0];
lastName = values[1];
mark = double.Parse(values[2]);
total += mark;
count++;
}
}
}
//close input file
sr.Close();
avg = total / count;
Console.WriteLine("nClass Average: " + avg);
// open an output file
StreamWriter sw = new StreamWriter(OUTPUT_FILE);
sw.WriteLine(avg);
sw.Close();

您可以包装读取和写入"IO"用"try…catch"块,以便在程序中出现错误时可以解决。

static void Main(string[] args)
{
Test();
}
public static void Test () 
{
try
{
// constant variables
const string INPUT_FILE = "input.txt";
const string OUTPUT_FILE = "output.txt";
// open the input file
StreamReader sr = new StreamReader(INPUT_FILE);
// fields used for input file
string? line = "";
string firstName = "", lastName = "";
double mark = 0;
// variables for calculating average
double total = 0, count = 0, avg = 0;
// continue to read until you reach end of file
while ((line = sr.ReadLine()) != null)
{
string[] values = line.Split(',');
firstName = values[0];
lastName = values[1];
mark = double.Parse(values[2]);
count++;
}
//close input file
sr.Close();
avg = total / count;
Console.WriteLine("nClass Average: " + avg);
// open an output file
StreamWriter sw = new StreamWriter(OUTPUT_FILE);
sw.WriteLine(avg);
sw.Flush();
sw.Close();
}catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

}

最新更新