Question about reading text-files

M

Mika M

Hi!

I'm trying to make something like this to work with VB.NET 2003...

Dim sr As StreamReader = New StreamReader("C:\MyFile.txt")
Dim strLine As String

While ((strLine = sr.ReadLine()) <> Nothing)
'// Doing something here
End While

....but can't get it to work. What's wrong with that?

I have studied some Java basic programming earlier, and in this case I'm
trying to make an idea from java code...

while ((strLine = br.readLine()) != null) // br = BufferedReader

....to convert to work using VB 2003.
 
M

Meelis Lilbok

Dim sr As StreamReader = New StreamReader("C:\MyFile.txt")
Dim strLine As String

strLine=sr.ReadLine()
While Not strLine Is Nothing
'//Do something here

input = sr.ReadLine()
End While
sr.Close()


Hope this helps!

Meelis
 
C

Cor Ligthert [MVP]

Mika,

The most easiest one in this case

\\\
Dim sr As StreamReader = New StreamReader("C:\MyFile.txt")
Dim strLine As String = sr.readtoend
///
However set it in a try and catch block for if the file does not exist.
\\\
dim strLine as String
Try
strLine = sr.ReadToEnd
Catch ex
messagebox.show(ex.tostring) 'in a more decent way
End Try
///

I hope this helps,

Cor
 
M

Meelis Lilbok

sry
input = sr.ReadLine() must be strLine=sr.ReadLine()


Meelis




Meelis Lilbok said:
Dim sr As StreamReader = New StreamReader("C:\MyFile.txt")
Dim strLine As String

strLine=sr.ReadLine()
While Not strLine Is Nothing
'//Do something here

input = sr.ReadLine()
End While
sr.Close()


Hope this helps!

Meelis
 
M

Mika M

Yes I know basic ways to read file. In fact I was just trying to join
reading line and checking if the last line was reached, to save code
lines because "simple is beautiful" in my mind :)

Obviously that's not possible when using VB.NET
 
H

Herfried K. Wagner [MVP]

Mika M said:
I'm trying to make something like this to work with VB.NET 2003...

Dim sr As StreamReader = New StreamReader("C:\MyFile.txt")
Dim strLine As String

While ((strLine = sr.ReadLine()) <> Nothing)
'// Doing something here
End While

The problem is that '=' is interpreted as comparison operator when being
used in the 'While' condition. Working solutions can be found here:

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

Guest

you could also try this way

Private sr as StreamReader = new StreamReader("C:\MyFile.txt")


do

me.textbox1.text = sr.readline()

loop while sr.peek > -1
 

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