'How can I re-write this input/output files so that my input file is a comma delimited file

How can I re-write this input/output files so that my input file is a comma delimited file (input.csv)?

Input file (input.csv):

Smith,John,87
Doe,Jane,93
Sacramanto,Bob,57

I'm a beginner and my teacher says we must use the line.indexOf() function.

// 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();


Solution 1:[1]

I think you made a little bit of confusion between line and values. line contains the actual line, values contains the separated values of the single line. You have to read the line once for each loop iteration. Try the code blelow

// 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();

Solution 2:[2]

You can wrap code that reads and writes "IO" with a "try...catch" block so that if there is an error in the program, it can be resolved.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marco Beninca
Solution 2