Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading file using c#.. truncates first character of every line 1

Status
Not open for further replies.

chennaiprogrammer

Programmer
Jul 31, 1999
54
private System.IO.StreamWriter sw;
private System.IO.StreamReader sr;
string path=@"E:\projects\file1.txt";



void MainFormLoad(object sender, System.EventArgs e)
{
sr=File.OpenText (path);
listBox1.Items.Clear ();
while((sr.Read ())!=-1)
{
listBox1.Items.Add (sr.ReadLine ());
}
sr.Close ();
}

}
}


Now if file1.txt contains

the data

firstline
secondline
thirdline
fourthline

The output comes as

irstline
econdline
hirdline
ourthline

The first character of each line gets truncated

Please advise how to overcome this problem ?

chris

 
Could the "while((sr.Read ())!=-1)" be eating the first character?

Try this instead:
// NOT TESTED/COMPILED!!!
string RL = sr.ReadLine();
while(RL != null)
{
listBox1.Items.Add(RL);
RL = sr.ReadLine();
}

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top