reading textfile

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,

I am reading in a textfile which looks like this (there is no new line after
the last number)

03 98661881
0407 566453

The code to load the textfile looks like this:

System.IO.Stream sio;
sio = fDocument.PostedFile.InputStream;
StreamReader sr = new StreamReader(sio);
string line;
while ((line = sr.ReadLine()) != null)
{
dcd.AddTechPhone(TextBoxNumber.Text ,line);
}

The problem is that it adds a blank line to the last record in the textfile.
How can I avoid it?

Thanks
Chris
 
Hello,
Try to use StreamReader's ReadToEnd method. It'll load whole content
of the file (including new line characters).
But wait a minute, are you experiencing problem in writing the contents
back to the file? if this is the case then try to use StreamWriter's
Write method instead of WriteLine. Remember to use Flush before closing.

HTH. Cheers :)

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
chris said:
I am reading in a textfile which looks like this (there is no new line after
the last number)

03 98661881
0407 566453

The code to load the textfile looks like this:

System.IO.Stream sio;
sio = fDocument.PostedFile.InputStream;
StreamReader sr = new StreamReader(sio);
string line;
while ((line = sr.ReadLine()) != null)
{
dcd.AddTechPhone(TextBoxNumber.Text ,line);
}

The problem is that it adds a blank line to the last record in the textfile.
How can I avoid it?

How sure are you that there isn't a newline after the last number? I
strongly suspect that there is, really. Either that or
AddTechPhoneNumber is adding a newline itself.
 
Back
Top