| | |
using a "for" loop to read a text file
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 4
Reputation:
Solved Threads: 0
hello! I need to use this for loop to extract specific data from a text file. But it gives me an error message:syntax error.I hope you don't mind helping. Thank you for your time and effort.
Below is my codes:
OpenFileDialog1.ShowDialog()
Dim a As String
TextBox2.Text = OpenFileDialog1.FileName
a = TextBox2.Text
Dim objStreamReader As New StreamReader(TextBox2.Text)
for each i as StreamReader
objStreamReader.ReadLine()
TextBox4.Text = (objStreamReader.ReadLine())
Label2.Text = TextBox4.Text.Substring(0, 3)
Next
objStreamReader.Close()
Below is my codes:
OpenFileDialog1.ShowDialog()
Dim a As String
TextBox2.Text = OpenFileDialog1.FileName
a = TextBox2.Text
Dim objStreamReader As New StreamReader(TextBox2.Text)
for each i as StreamReader
objStreamReader.ReadLine()
TextBox4.Text = (objStreamReader.ReadLine())
Label2.Text = TextBox4.Text.Substring(0, 3)
Next
objStreamReader.Close()
•
•
Join Date: Mar 2006
Posts: 12
Reputation:
Solved Threads: 0
hi randycena,
I am not sure of the purpose of your code as your description is brief and I assumed that you wish to read a text file and extract certain string from the file. If we just take the for loop portion of the code, there maybe some problem. You're defining the streamreader object for every loop you go through which is not so correct. The stremreader object can read a whole stream at one go. You should use the objStreamReader you defined earlier as this instance has been 'tied' to the text file defined in textbox2.text. So instead of using For..Next loop, you should you While..End While and use the EndOfStream property to check if it's end of stream.
While NOT objStreamReader.EndOfStream
'do whatever you want to do
End While
I am not sure of the purpose of your code as your description is brief and I assumed that you wish to read a text file and extract certain string from the file. If we just take the for loop portion of the code, there maybe some problem. You're defining the streamreader object for every loop you go through which is not so correct. The stremreader object can read a whole stream at one go. You should use the objStreamReader you defined earlier as this instance has been 'tied' to the text file defined in textbox2.text. So instead of using For..Next loop, you should you While..End While and use the EndOfStream property to check if it's end of stream.
While NOT objStreamReader.EndOfStream
'do whatever you want to do
End While
![]() |
Similar Threads
- Read text file to a certain point (C#)
- read text file (C)
- read text file (C#)
- Homework: filling array from text file (VB.NET)
- how do i read the last line of a text file? (Python)
Other Threads in the VB.NET Forum
- Previous Thread: VB.net code problem
- Next Thread: using VB2003 to accept excel file and convert it into a graph
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2005 2008 access account arithmetic array basic binary bing button buttons center check code component connectionstring crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dissertationthesis dropdownlist excel fade file-dialog folder ftp generatetags hardcopy image images input insert intel internet login math mobile monitor ms navigate net networking objects opacity output passingparameters peertopeervideostreaming picturebox picturebox1 port problem problemwithinstallation project reports" savedialog searchvb.net select serial shutdown soap string survey tcp temperature text textbox timer toolbox trim update updown user usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf





