Home > other >  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

Time:05-03

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

CodePudding user response:

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

CodePudding user response:

I put the parameters up, what I'm not sure about is whether it can be accumulated normally, thank you

    public static void readandwriteCSV(string INPUT_FILEe,string OUTPUT_FILEe) 
    {
   try{

  // open the input file
   StreamReader sr = new StreamReader(INPUT_FILEe);
  // open an output file
  StreamWriter sw = new StreamWriter(OUTPUT_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;

  while((line = sr.readLine()) != null){

    string[] values = line.split(",");
    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);
sw.WriteLine(avg);
sw.Close();
}catch(Exception e){
Console.WriteLine("An error occurred")
 }
}
  • Related