sabine

sabine

  • NA
  • 11
  • 0

Read all lines of a text file

Apr 4 2012 9:19 AM
Hi,
I'm using the below code to read a text file.
int counter = 0;
                string readLine;
                string accountName= "";
                string contactFName = "";
                string contactLName ="";
               
                StreamReader tr = new StreamReader("text.txt");
                // Read header
                tr.ReadLine();
                counter++;
                while((readLine = tr.ReadLine()) != null)
                {
                    tr.ReadLine();
                    readLine = readLine.Trim();
                    if (readLine == "") continue; // ignore blank lines
                    string[] items = readLine.Split(';');
                    accountName = items[0];
                    contactFName = items[1];
                    contactLName = items[2];

                    Console.WriteLine("attr1 = {0}, attr2 = {1}, attr3 = {2}, attr4 = {3}" + accountName + contactFName + contactLName);

                    counter++;
                }
                tr.Close();

I am having the header and two lines of data but the issue is that this code is reading only 1 line of data.
How can I make it loop on all the data lines?
Thank you in advance

Answers (2)