Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

StreamReader.ReadLine to ignore carriage returns?

1,007 views
Skip to first unread message

VBDotNet Team [MS]

unread,
Oct 22, 2002, 4:04:06 PM10/22/02
to
Joshua,

the StreamReader defines a line as a sequence of characters followed by a
line feed ("\n"), carraiage return ("\r") or a carriage return immediately
followed by a line feed ("\r\n"). In your case every other line is probably
just an empty line, corresponding to the carriage return that starts every
line. Maybe you can just ignore empty lines? In this case you don't need any
additional code to handle your input properly.

Sunder, SDET & Karol, SDE
VB.Net

--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Joshua Campbell" <Joshua....@WellmanInc-DieSpamDie.com> wrote in
message news:OXOv6XfeCHA.1704@tkmsftngp10...
> Is there a way to get the ReadLine command to just look for Line Feeds? I
> am having to build a program to parse out text files for an order
processing
> system. The files are being sent via FTP from a VAX, and somehow there
are
> carriage returns at the beginning of every line and a line feed at the end
> of every line. (Yes, I know, seems stupid, but that's what it does.)
>
> Alternatively, if there is no way to do this, I guess that I am going to
> have to go character by character through the file and compare the
> characters to a carriage return and a line feed. How can I do this?
>
> TIA!
> Joshua
>
>
>


Joshua Campbell

unread,
Oct 23, 2002, 7:17:02 AM10/23/02
to
Unfortunately, I can't just double things. The lines that are legitimately
blank have a carriage return followed by a line feed. So, ReadLine counts
this as one line. The lines that have text on them have a carriage return
at the beginning, and a line feed at the end, which is interpreted as two
lines by ReadLine.

Is there a way to get the length of the new line? I have only found a way
to get the length of the whole stream.

Would a compare for a carriage return be like this:
If strString like (chr(13)) then
...

"VBDotNet Team [MS]" <vbdo...@microsoft.com> wrote in message
news:eqBk#XgeCHA.1252@tkmsftngp08...

Bharat Patel [MS]

unread,
Oct 23, 2002, 2:21:44 PM10/23/02
to
Hi Joshua,

ReadLine method returns a string. If you check the length of this string
inside the while loop, you should be able to ignore it easily if the length
of the string is <1. If you still need some help, please provide the sample
text file which you are trying to read.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.

Please reply to newsgroups only. Thanks.

Bharat Patel [MS]

unread,
Oct 24, 2002, 1:00:15 PM10/24/02
to
Hi Joshua,

The attached file does not seem to duplicate the issue. I tested it with
the following code and it came out OK.
I also tested it with another file which I created manually using binary
file IO in VB6 and it pulled out only the lines which had some information.
Here is the sample code.

Dim reader As StreamReader = _
New StreamReader("C:\Temp\file.txt")
Dim sLine As String
Try
Me.TextBox1.Clear()
Do 'Until reader.Peek = -1
sLine = reader.ReadLine
If sLine.Length > 0 Then
TextBox1.Text &= sLine & ControlChars.CrLf
End If
Loop Until reader.Peek = -1

Catch
TextBox1.Text = "File is empty"
Finally
reader.Close()
End Try

0 new messages