I have an input text file with data in 4 columns. I want to put this data into an array arr((1 to 20), (1 to 4)) how do i write the 4 columns of data to the specific colum in the array - note the columns are separated by spaces?
Printable View
I have an input text file with data in 4 columns. I want to put this data into an array arr((1 to 20), (1 to 4)) how do i write the 4 columns of data to the specific colum in the array - note the columns are separated by spaces?
Something along these lines, presuming you are using Visual Basic 6 so you can use the Split Function.
Code:Private Sub Command1_Click()
Dim tstArray(1 To 20, 1 To 4) As String
Dim intfilenum As Integer
Dim fileName As String
Dim splitArray() As String
Dim counter As Integer
Dim x As Integer
Dim tempString As String
fileName = "c:/testfile.txt"
intfilenum = FreeFile
Open fileName For Input As #intfilenum
counter = 1
Do Until EOF(intfilenum)
Line Input #intfilenum, tempString
splitArray = Split(tempString, " ")
tstArray(counter, 1) = splitArray(0)
tstArray(counter, 2) = splitArray(1)
tstArray(counter, 3) = splitArray(2)
tstArray(counter, 4) = splitArray(3)
counter = counter + 1
Loop
Close #intfilenum
For x = 1 To UBound(tstArray)
Debug.Print tstArray(x, 1) & " " & tstArray(x, 2) & " " & _
tstArray(x, 3) & " " & tstArray(x, 4)
Next x
End Sub