473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Checking for EOF with Streamreader

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
5 23181
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Keith Kingsley | last post by:
I'm using a StreamReader to read in several lines from an ASCII file. I'd like to know the StreamReader's "true" position-- that is, the number of bytes into the file that the StreamReader has...
3
by: Arno | last post by:
Hi, I'm using TcpClient for communication between two PC running a small piece of software. The protocol used has been designed internally and is HTTP similar (command line, headers, body). A...
4
by: Peter | last post by:
Currently I'm using the method below, is there someting more efficient?: Imports System.IO Public Class CountLine Public Shared Function CountLines(ByVal FileName As String) As Integer Dim fs...
4
by: Just Me | last post by:
I need to get a StreamReader Dim lsr0 As New StreamReader(FullPath) If the file does not exist I'd like it to create one. Do I have to Try/Catch or is there a clean way of doing it?
3
by: Dave Cullen | last post by:
How does one check for end-of-file while using the Streamreader class methods? File opened like so... inputstream = File.OpenRead(Infile) Dim SrRead As StreamReader = New...
4
by: Hexman | last post by:
Code below ---- I've asked a similar question on this forum earlier. This is a slightly different situation. Previous Question ---- I'm trying to save some specific web pages to disk as...
1
by: AMP | last post by:
Hello, I am trying to see if a FileStream Opened using an"if" clause: FileStream infile = new FileStream("c:\\blink2.txt", FileMode.Open, FileAccess.Read, FileShare.Read); if (infile==0)..... ...
5
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
0
by: rajana | last post by:
Dear All, We have Ansi file with german characters (Ä / Ø) , We are using Streamreader to read the contents of the file. But Readline() not able to read the German characters. We tried all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.