Checking for EOF with Streamreader

L

Lyle

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_2005_8_0_22_0/dotnet-languages-vb.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
 
S

Siva M

Check the EndOfStream property.

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_2005_8_0_22_0/dotnet-languages-vb.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
 
C

Chris Dunaway

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.
 
L

lyle

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_2005_9_0_0_603453/Checking-for-EOF-with-Streamreader.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
 
C

Chris Dunaway

lyle said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top