• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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\";

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

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

                  foreach(object s in dummies)


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.

1 Solution
instead of

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

write this:

foreach(string s in columns)
    scores += s;
Mike TomlinsonMiddle School Assistant TeacherCommented:
"how do I make the parser read through all the columns in the line after the 0th column?"

So you want to skip the first element?...

            for (int i = 1; i < columns.Length; i++)
                scores += columns[i];

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now