2D Array read from Text file. Question 4 2/26/2013 8:39:10 PM 3/6/2013 8:01:08 AM Discuss and ask questions about the C# programming. you can even make the original List into an Array of string arrays. . Read From a Text File (C# Programming Guide). see How to: Read a Text File One Line at a Time (Visual C#). Note. // Example #2 // Read each line of the file into a string array. . text file after to convert to array then sort records ascending or descending as well as you can set starting a. This Tutorial helps to learn Read data's from text file after. C# Reading from a text file. I have a text file that looks like the following: - Blackpool, 4 Liverpool, 5 Blackpool, 2 Liverpool, 3 Basically, what it is going to be a league table consisting of a team and a total score. I have split them added the two. Sort a Text File in C#namespace League. Table. public partial class Frm_League. Tbl : Form. public Frm_League. Tbl(). Initialize. Component(). } //Declare Constantsconst int ARRAY_SIZE = 8; const int EOF_IDENTIFIER = 0; //Declare Variablestring txt. From. File = ""; //Declare Teams Arraystring[] Teams = new string[ARRAY_SIZE]; //and a corresponding totals arrayint[] Total. Scores = new int[ARRAY_SIZE]; public void Read. Team. Names(){// create reader & open file. Text. Reader TR_Teams = new Stream. Reader("Teamname.
So you need a class to hold the values : public class myInfo {public int LineNumber; public string FirstName; public string LastName; public double Value1; public double Value2; public double Value3;} Select all Open in new. Declare Array Pointerint Array. Pointer = 0; //While not EOFwhile (TR_Teams. Peek() > EOF_IDENTIFIER){ //Read a linetxt. From. File = TR_Teams. Read. Line(); //Write line from text file to the array. Teams[Array. Pointer] = txt. From. File; //Increment Array Pointer. Array. Pointer++; } //Close the stream. TR_Teams. Close(); } public void Read. Results() {// create reader & open file. Text. Reader TR_Results = new Stream. Reader("Results. txt"); //Declare Array Pointerint Array. Pointer = 0; //While not EOFwhile (TR_Results. Peek() > EOF_IDENTIFIER){ //Read (each) linetxt. From. File = TR_Results. Read. Line(); //Split the string into two portionsstring[] Split. Result = txt. From. File. Split(new Char[] { ',' }); //Now search the array. ARRAY_SIZE; i++){. If the team name matches the current fixtureif (Teams[i]== Convert. To. String(Split. Result[0])){//Add this fixture's score to that already existing. Total. Scores[i] = Total. Scores[i] + Convert. To. Int. 32( Split. Result[1] ). } } } //Close the stream. TR_Results. Close(); } private void Btn_Close. League. Tbl_Click(object sender, Event. Args e){//Close this (.. Close(); } private void Btn_Results_Click(object sender, Event. Args e){//Reset Text box. Txt_Team. Results. Text = ""; //Reset arrays: for (int j = 0; j < ARRAY_SIZE; j++){Teams[j] = ""; Total. Scores[j] = 0; }//Read Team names into the Teams array. Read. Team. Names(); //Read Results Results into the Results array. Read. Results(); //Send the array out to the output text box for (int i = 0; i < ARRAY_SIZE; i++){ Txt_Team. Results. Text = Txt_Team. Results. Text + Teams[i] + " " + Total. Scores[i] + "\r\n"; } } }}.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2016
Categories |