473,408 Members | 2,442 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,408 software developers and data experts.

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

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.....
Sep 8 '08 #1
5 3998
kenobewan
4,871 Expert 4TB
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.
Sep 8 '08 #2
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 :)
Sep 8 '08 #3
Curtis Rutland
3,256 Expert 2GB
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.
Sep 8 '08 #4
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 :)
Sep 8 '08 #5
balabaster
797 Expert 512MB
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.
Sep 8 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
3
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
16
by: Roy | last post by:
I use a Access 2K application.I am trying to use Chuck Grimsby(clsReadTextFile.txt)class utility to read a text file and then do a import of the same into my database.The question is how to call...
2
by: chris | last post by:
Hi there, I am reading in a textfile which looks like this (there is no new line after the last number) 03 98661881 0407 566453 The code to load the textfile looks like this:
3
by: booksnore | last post by:
I have to read data from a flat file with millions of records. I wanted to find the most efficient way of doing this. I was just going to use a StreamReader and then break up the input line using...
1
by: Justin Fancy | last post by:
Hi everyone, I have a textfile which I need to read and compare dates. The text file summarizes every time I do an update to an internet site. Sample output is as follows: Copying...
3
by: psbasha | last post by:
Hi , When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ). ...
3
by: Arn | last post by:
I just started to learn C++ and have some problem when I read a hole line from a textfile. I would be grateful if anyone can tell me what is wrong with my code. I'm using Borland Developer Studio...
6
by: Per Juul Larsen | last post by:
Hi. My Textfile looks like this Variable 1 Variable 2 Variable 3 Variable 4 On opening the application it will read the textfile, and assign each line of variable to at textfield in VB and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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...
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
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.