Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have to read a text file and load the info into my program

Amy wong / Glasgow
Jane foster / Newcastle
1,3,2
2,1,2
3,4,9

So far I have

StreamReader LiveData = new StreamReader(Convert.ToString(openFileDialog1.FileName));
label2.Text = LiveData.ReadLine();
label4.Text = LiveData.ReadLine();

This gets the names in the right place LOL.

I need to load the data into usable strings for my program.

the first two are the player names. The lines underneath represent round,player1,player2.

I need
string player1
string player2
string round
string score1
string score2

this is just giving me a headache I can read the data but have no idea how I can filter it. My program is quite simple it displays the data to the user. Its purpose is for displaying results of games on a projector.

My original program allowed manual entry of scores but if I could automaticly read this file every few seconds it would be much better. I have been programming for a while now but I find interpreting text information hard.
Posted
Updated 10-Apr-12 6:53am
v3
Comments
Clifford Nelson 10-Apr-12 13:21pm    
Is there some reason your text format is like it is. Sounds like this is data saved from your application, if you could use XML serialization, it would work better.

Try something like this:

C#
StreamReader LiveData = new StreamReader(Convert.ToString(openFileDialog1.FileName));
string[] data = LiveData.ReadLine().Split(',');//Reads a line and splits it at the delimiter (',' in this case) into an array of strings.

//You can then process the strings in the array

LiveData.Close();
 
Share this answer
 
Comments
VJ Reddy 15-May-12 10:51am    
My 5!
BillW33 15-May-12 20:17pm    
Thanks, VJ :)
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.
C#
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;
    }
}
//Output from the above code:
//Players:
//--Amy wong / Glasgow
//--Jane foster / Newcastle
//Round: 1
//---Score1:3
//---Score2:2
//Round: 2
//---Score1:1
//---Score2:2
//Round: 3
//---Score1:4
//---Score2:9

LINQPad, which can be downloaded from here http://www.linqpad.net/[^], can be used for quick test of the above code.
 
Share this answer
 
v3
Comments
A-s-h-l-e-y 10-Apr-12 14:25pm    
thanks to everyone for the help :)
VJ Reddy 10-Apr-12 19:39pm    
You're welcome.
BillW33 15-May-12 20:18pm    
Good answer, deserves a 5 :)
VJ Reddy 15-May-12 20:50pm    
Thank you, CIDev :)
write something more to identify players name, round and score. then you will easily read text from the text file.
 
Share this answer
 
Comments
A-s-h-l-e-y 10-Apr-12 12:40pm    
The file is written by another application. I can't edit the output or i would use XML.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900