Massaging Data in a Text File -

How to extract data line by line from a text file into an array

Page 1 of 1

1 Replies - 2804 Views - Last Post: 18 December 2008 - 01:04 PM Rate Topic: -----

#1 hillbma  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 08-December 08

Massaging Data in a Text File -

Posted 18 December 2008 - 11:50 AM

Good day,

I am trying to import data from a text file into an array using VB.Net 2005. What I want to do is parse the data in VB.Net and then export the data back to a text file. The problem I am having is I am not quite sure how to get each line of data brought in a a separate array element. From doing searches I have seen refernces to vbctrlf but haven't been able to find out how to apply it in my case.

My code so far is as follows:

' Project name:	 Summarize Multiple Transaction Exports by Client Number Project
' Project purpose:  The project imports transactions and summarizes all data by the Client Number,
'					
'				   
' Created/revised:  Me on December 17, 2008

Option Explicit On
Option Strict On

Public Class MainForm


	Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		' load from the text file the product item numbers nad prices

		' declare the variables used
		Dim inputfile As String = "Exports.txt"
		Dim filecontents As String
		Dim lineindex As Integer


		' check to make sure the file exists, and if yes then
		' get the contents of the file. If not, display message.

		If My.Computer.FileSystem.FileExists(inputfile) Then
			filecontents = My.Computer.FileSystem.ReadAllText(inputfile)
		Else
			MessageBox.Show("File does not exist", "Exports.txt", _
				MessageBoxButtons.OK, MessageBoxIcon.Information)
		End If

		'find first newline in string 
		lineindex = filecontents.IndexOf(ControlChars.NewLine)

		' dimension array to store each line in the Exports.txt file
		Dim transaction(lineindex) As String
		Dim counter As Integer = 0

		' run through data to store each transaction in the transaction array
		' Do Until lineindex - counter <= 0
		'	transaction(counter)=
		'
		'	   Loop
End sub
End Class




Can anyone point me in the right direction for bring each line of the text file into an array element?

so for exaple, if my test file contains:

546.34 (tab delimiter) 123456
745.35 (tab delimiter) 567890
123.45 (tab delimiter) 987654

I would want the array to have:

transaction(0)=546.34 (tab delimiter) 123456
transaction(1)=745.35 (tab delimiter) 567890
transaction(2)=123.45 (tab delimiter) 987654

After I get everything into the array, I'll parse the data with string manipulation....which I think I can do....and then I'll export the data back to a text file.....which also, I think I can do.

Thanks in advance for any assistance!

Is This A Good Question/Topic? 0
  • +

Replies To: Massaging Data in a Text File -

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Massaging Data in a Text File -

Posted 18 December 2008 - 01:04 PM

Try
...
//' dimension array to store each line in the Exports.txt file
//'Dim transaction(lineindex) As String

Dim transaction() As String = Split(filecontents, vbCrLf)
Dim counter As Integer = 0
...


transaction.Length will give you the number of strings in the array.

This post has been edited by n8wxs: 18 December 2008 - 01:07 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1