By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

Checking for EOF with Streamreader

P: n/a
Hi. What is the best way to check for EOF when using the streamreader? I am using a do loop. Following is what I have tried but all end in an error when eof is reached.

1. Adding 'until line is nothing' to the end of the loop.
2. Adding 'while not line is nothing' after the do.
3. After the line is read in the do coding:
If line Is Nothing Then
Exit Do
End If

Thanks for your help!

From http://www.developmentnow.com/g/38_2...nguages-vb.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Check the EndOfStream property.

"Lyle" <no****@developmentnow.com> wrote in message
news:c4**********************************@msnews.m icrosoft.com...
Hi. What is the best way to check for EOF when using the streamreader? I
am using a do loop. Following is what I have tried but all end in an error
when eof is reached.

1. Adding 'until line is nothing' to the end of the loop.
2. Adding 'while not line is nothing' after the do.
3. After the line is read in the do coding:
If line Is Nothing Then
Exit Do
End If

Thanks for your help!

From
http://www.developmentnow.com/g/38_2...nguages-vb.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
Nov 21 '05 #2

P: n/a
If you're using the Read method, it will return 0 at the end of stream.
If you're using ReadLine, the string returned will be Nothing if at
end of stream.

You can also try checking the Position properter of the BaseStream
against the length of the stream.

It would be helpful if you told us what the error is or showed some
actual code.

Nov 21 '05 #3

P: n/a
I am using the readline method. Here a condensed version of my code...

1. Method 1 - Adding 'until line is nothing' to the end of the loop.

Do
line = infilereader.ReadLine()

co = line.Substring(25, 4)
emp = line.Substring(30, 4)
Loop until line is nothing

2. Method 2 - Adding 'while not line is nothing' after the do.

Do until line is nothing
line = infilereader.ReadLine()

co = line.Substring(25, 4)
emp = line.Substring(30, 4)
Loop

3. Method 3 - Using an Exit Do

Do
line = infilereader.ReadLine()

If line Is Nothing Then
Exit Do
End If

co = line.Substring(25, 4)
emp = line.Substring(30, 4)

Loop

THANKS!
From http://www.developmentnow.com/g/38_2...reamreader.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
Nov 21 '05 #4

P: n/a
"Lyle" <no****@developmentnow.com> schrieb:
What is the best way to check for EOF when using the streamreader?


Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #5

P: n/a
lyle wrote:
I am using the readline method. Here a condensed version of my code...

1. Method 1 - Adding 'until line is nothing' to the end of the loop.

Do
line = infilereader.ReadLine()

co = line.Substring(25, 4)
emp = line.Substring(30, 4)
Loop until line is nothing
If line is nothing after the call to ReadLine here, then when you call
Substring, it will throw and exception. You should at least check line
to make sure it is not nothing.

2. Method 2 - Adding 'while not line is nothing' after the do.

Do until line is nothing
line = infilereader.ReadLine()

co = line.Substring(25, 4)
emp = line.Substring(30, 4)
Loop
Same thing in number 2 as number 1. If line is nothing, you will
generate an exception.

3. Method 3 - Using an Exit Do

Do
line = infilereader.ReadLine()

If line Is Nothing Then
Exit Do
End If

co = line.Substring(25, 4)
emp = line.Substring(30, 4)

Loop


This is better because you check the line, but I personally like a
while loop:

line = infilereader.ReadLine()
While Not (line is nothing)
co = line.Substring(25,4)
emp = line.SubString(30,4)
line = infileReader.ReadLine()
End While

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.