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.ToStr ing());
}
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.
private void btnFile_Click(object sender, System.EventArgs e)
{
string scores = "";
string dir = @"C:\C#.NET\Files\";
if(!Directory.Exists(dir))
Directory.CreateDirectory(
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.ToStr
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
---> while(columns[x] != null)
{
scores += columns[x].ToString();
x++;
}
write this:
foreach(string s in columns)
scores += s;