Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading a textfile line by line and storing it in variable using vb.net

Newbie
 
Join Date: Jul 2008
Posts: 30
#1: Sep 8 '08
hi,

I can able to read the text file, following is the code for your reference......

Expand|Select|Wrap|Line Numbers
  1.         Dim file As New System.IO.StreamReader("E:\Dialer.txt")
  2.         Dim readLine As String = file.ReadLine
  3.         Dim I As Integer = 1
  4.         Do Until file.ReadLine Is Nothing
  5.             Dim s As String = readLine
  6.             MessageBox.Show(s)
  7.             I += 1
  8.         Loop
  9.  
In this code messagebox displays only the first line three times...... please let me know how to display each line in messagebox......


Thanks in advance.....

kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#2: Sep 8 '08

re: Reading a textfile line by line and storing it in variable using vb.net


Using s.length will enable you to display a certain number of characters. If there is a distinquishing space or chars afterwards you may be able to use s.split.
Newbie
 
Join Date: Jul 2008
Posts: 30
#3: Sep 8 '08

re: Reading a textfile line by line and storing it in variable using vb.net


Quote:

Originally Posted by kenobewan

Using s.length will enable you to display a certain number of characters. If there is a distinquishing space or chars afterwards you may be able to use s.split.

Hi,

Thanks for your reply.....

first i ll tell me required output......

say for example following is the textfile which im reading

Expand|Select|Wrap|Line Numbers
  1. 9954552067,abc.wav
  2. 1234567890,def.wav
  3. 7879989907,abc.wav
  4. 8989867890,def.wav
  5.  

my messagebox should display each line one by one..... but its displaying only first line ..... Please let me know how to get my output....... Please .... its urgent....

Thanks :)
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: Sep 8 '08

re: Reading a textfile line by line and storing it in variable using vb.net


The best way to do this is to do a .ReadToEnd() to read the whole file in, and then .Split it once you have it in a string.

So, something like this (from a recent project I did)
Expand|Select|Wrap|Line Numbers
  1. Dim sr As New StreamReader("C:\dev\test.txt")
  2. Dim read As String = sr.ReadToEnd()
  3. Dim lines() As String = read.Split(vbCrLf)
  4. For Each line As String In lines
  5.     MessageBox.Show(line)
  6. Next
  7.  
Note, depending on your file, you may have to change vbCrLf to vbCr or vbLf, depending on how the lines are broken. Just try each one to see which works.
Newbie
 
Join Date: Jul 2008
Posts: 30
#5: Sep 8 '08

re: Reading a textfile line by line and storing it in variable using vb.net


Quote:

Originally Posted by insertAlias

The best way to do this is to do a .ReadToEnd() to read the whole file in, and then .Split it once you have it in a string.

So, something like this (from a recent project I did)

Expand|Select|Wrap|Line Numbers
  1. Dim sr As New StreamReader("C:\dev\test.txt")
  2. Dim read As String = sr.ReadToEnd()
  3. Dim lines() As String = read.Split(vbCrLf)
  4. For Each line As String In lines
  5.     MessageBox.Show(line)
  6. Next
  7.  
Note, depending on your file, you may have to change vbCrLf to vbCr or vbLf, depending on how the lines are broken. Just try each one to see which works.


Thanks a lot :)

You gave me the solution exactly for what i wanted......


Arun :)
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#6: Sep 8 '08

re: Reading a textfile line by line and storing it in variable using vb.net


Quote:

Originally Posted by insertAlias

The best way to do this is to do a .ReadToEnd() to read the whole file in, and then .Split it once you have it in a string.

So, something like this (from a recent project I did)

Expand|Select|Wrap|Line Numbers
  1. Dim sr As New StreamReader("C:\dev\test.txt")
  2. Dim read As String = sr.ReadToEnd()
  3. Dim lines() As String = read.Split(vbCrLf)
  4. For Each line As String In lines
  5.     MessageBox.Show(line)
  6. Next
  7.  

I'd say that the current code isn't too far from correct... a couple of changes:

Firstly, in your While clause, you're saying:

While sr.Readline <> Nothing; This is causing the software to read the next available line. Then immediately after that, you're asking your software to read another line into a variable. So:

While sr.ReadLine = Nothing 'Read the next available line and move read cursor.
Dim current As String = sr.ReadLine - now we're reading another line, without doing anything with the line we read in the While declaration. Make sense?

I would make 2 slight changes to your code if it were me writing it:

While Not sr.EndOfStream

Dim sCurrentLine = sr.ReadLine()
MsgBox sCurrentLine

End While

I'd hazard a guess that currently something is tripping his loop up so it thinks that line 4 returns nothing. Maybe the line is blank. Maybe somehow the file is corrupt.

Unless you're actually using your "i" counter, don't even bother with it. With a "For...Next", you don't need a counter, declared other than in the For declaration. i.e. something like:

For i = 0 To sr.LastLineIndex() 'That's pseudo code, there's really no LastLineIndex() method, I'm just demonstrating a concept where your counter would be useful. Really, you should pick the right tool for the right job, and unless you really need to know what i is, I'd say stick with your While loop and ditch the counter variable as it's surplus to requirement.

In a "For Each...Next" an iterator is automatically implied, so you don't even need a counter... but if you need to know which line you're currently at, then you'll need to handle that with a separate variable. If this is the case, I'd really suggest a For...Next loop, rather than a For Each...Next. When you're looking for a specific event such as an end of file, you don't need a counter unless you need to know what iteration you are currently at - for instance if you need the line number.

In this situation, I'd say you're using the right loop type, you're just not implementing it quite correctly.
Reply