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
Loading
SenthilkumarPosted Apr 4, 2012, 11:33 AM
The issue is you reading the line twice and it will take the second line. It reads one line in the while entry and you need to process it, but again you are the next row inside.
VulpesPosted Apr 4, 2012, 9:20 AM
http://www.c-sharpcorner.com/Forums/Thread/165687/read-attributes-from-text-file.aspx