The following code can be used to read all the lines from the file into a
string array
, then the players are stored in an array and the round and score info is stored in a dictionary for easy retrieval using the key for later use in the program. The solution works for players in the first two lines and then repeating round and score data, as the score data is read in a loop.
void Main()
{
string[] scoresFromFile = System.IO.File.ReadAllLines(openFileDialog1.FileName);
string[] players = {scoresFromFile[0],scoresFromFile[1]};
Dictionary<int,Score> scores = new Dictionary<int,Score>();
string[] tempScores;
for(int i=2; i<scoresFromFile.Length; i++){
tempScores = scoresFromFile[i].Split(',');
scores.Add(Convert.ToInt32(tempScores[0]), new Score(Convert.ToInt32(tempScores[1]),Convert.ToInt32(tempScores[2])));
}
Console.WriteLine ("Players:");
Console.WriteLine ("--{0}\n--{1}",players[0],players[1]);
foreach (int key in scores.Keys)
{
Console.WriteLine ("Round: {0}",key);
Console.WriteLine ("---Score1:{0}\n---Score2:{1}",scores[key].Score1,scores[key].Score2);
}
}
public struct Score {
public int Score1;
public int Score2;
public Score (int score1, int score2){
Score1=score1;
Score2=score2;
}
}
LINQPad, which can be downloaded from here
http://www.linqpad.net/[
^], can be used for quick test of the above code.