473,883 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 23318
Check the EndOfStream property.

"Lyle" <no****@develop mentnow.com> wrote in message
news:c4******** *************** ***********@msn ews.microsoft.c om...
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.Re adLine()

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.Re adLine()

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

3. Method 3 - Using an Exit Do

Do
line = infilereader.Re adLine()

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****@develop mentnow.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&la ng=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.Re adLine()

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.Re adLine()

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.Re adLine()

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.Re adLine()
While Not (line is nothing)
co = line.Substring( 25,4)
emp = line.SubString( 30,4)
line = infileReader.Re adLine()
End While

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13027
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 read. I thought about using MyStreamReader.BaseStream.Position, but this always seems to return a multiple of the StreamReader's buffer size (which seems natural-- as I understand it the StreamReader reads from the underlying stream in discrete...
3
5025
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 typical response in this protocol would be:
4
1820
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 As IO.FileStream Dim sr As IO.StreamReader Dim Result As Integer Try fs = New IO.FileStream( _
4
3648
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
1686
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 StreamReader(inputstream, _ System.Text.Encoding.ASCII) I intend to do-while not EOF using SrRead.Readline and I can't figure
4
10911
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 text files. I searched the Internet and found a basic example which I changed to fit my needs. I tested it out first on a single URL and it worked fine. Now when I incorporate an array of URL's, it fails to work. The first "responseFromServer"...
1
11689
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)..... but that doesnt work (they dont compare) ALSO, FileStream(......) is a constructor, but behaves like a Method. I'm a
5
2224
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 Import_File_Reader As System.IO.StreamReader While ...
0
2367
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 possibilities of calling the streamreader, but nothing worked. Dim sr As StreamReader = New StreamReader(Filename, System.Text.Encoding.Default, True) Dim sr As StreamReader = New StreamReader(Filename, _System.Text.Encoding.ASCII, False, 512)
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9777
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10833
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5782
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.