Link to home
Start Free TrialLog in
Avatar of igor92128
igor92128

asked on

Reading Lines from a file

Here is my code so far:

private void btnFile_Click(object sender, System.EventArgs e)
{
      string scores = "";
      string dir = @"C:\C#.NET\Files\";
      if(!Directory.Exists(dir))
      Directory.CreateDirectory(dir);

      string path = dir + "studentlist.txt";
      StreamReader textIn = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read));

      ArrayList dummies = new ArrayList();
      while(textIn.Peek() != -1)
      {
            string row = textIn.ReadLine();
            string[] columns = row.Split('|');
                  
            int x = 1;

               
--->            while(columns[x] != null)
            {
                  scores += columns[x].ToString();
                  x++;
            }

            Student student = new Student(columns[0], columns[1]);
      //      student.Name = columns[0];
      //      student.Scores = columns[1];
            dummies.Add(student);
      }

                  foreach(object s in dummies)
      {
            mainList.Items.Add(s.ToString());
      }
      textIn.Close();

}

I want the parser to read the line, which is in the format of "Someone's Name|54|65|76|76". So for column[0] we have the name, and columns[1] and higher (number can vary), we have the scores. In the line of code indicated, how do I make the parser read through all the columns in the line after the 0th column? I can't quite get it working.

I can only get the listbox to display "Someones Name|44", but I need to display all the numbers after that too.

thanks.
Avatar of HeidarV
HeidarV

instead of

--->          while(columns[x] != null)
          {
               scores += columns[x].ToString();
               x++;
          }

write this:

foreach(string s in columns)
    scores += s;
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial