PDA

Click to See Complete Forum and Search --> : Text File to Arrays


exx_maverick
Dec 28th, 1999, 08:49 PM
Simply stated, I want to read a text file with data into an array for use in calculations. I am not an experienced programmer, but this is a pretty straight forward procedure in FORTRAN, but I am beside myself with the process in VB 6. The books I have and sites I've read are great, but they don't deal specifically with reading in data like this or they leave a critical element out, which may be obvious to them but for the uninitiated it is a major obstacle.

I am using the text file as a source which can be updated/modified in order to leave the main program the same. It is a 44 by 51 array (44 columns and 51 rows).

Appreciate any feedback or help.

Dec 28th, 1999, 09:48 PM
Here is some basic code. It assumes a linefeed at the end of each textline and that the data is seperated by a space. This of course can be modified to fit your needs.

Dim TableData(44,51)
Dim InputFile as String
Dim iFileNum as Integer
Dim InputString as String
Dim TempString as String
Dim CurrentRecord as Long
Dim CurrentFile as Long
Dim EndCount as Long
Dim BeginCount as Long
Dim EndCount2 as Long
Dim BeginCount2 as Long
Dim x as Long
Dim z as Long

LineFeed = ChrW(13)
InputFile = "c:.blah/blah.txt"
iFileNum = FreeFile
Open InputFile For Input As iFileNum
InputString = Input(LOF(iFileNum), iFileNum)
CurrentRecord = 0
CurrentFile
BeginCount = 1
For x = 1 To Len(InputString)
If Mid(InputString, x, 1) = LineFeed Then
EndCount = x - 1
CurrentFile = CurrentFile + 1
If EndCount > BeginCount Then
TempString = Mid(InputString, BeginCount, EndCount - BeginCount)
BeginCount2 = 1
For z = 1 to Len(TempString)
If Mid(TempString, z, 1) = “ “ Then
EndCount2 = z - 1
CurrentRecord = CurrentRecord + 1
If EndCount2 > BeginCount2 Then
TableData(CurrentFile,CurrentRecord) = Mid(TempString, BeginCount2, EndCount2 – BeginCount2)
End If
End If
Next z
End If
BeginCount = x + 1
End If
Next x

Dec 28th, 1999, 10:25 PM
Sorry this is better.

Dim TableData(44,51)
Dim InputFile as String
Dim iFileNum as Integer
Dim InputString as String
Dim TempString as String
Dim CurrentRecord as Long
Dim CurrentFile as Long
Dim EndCount as Long
Dim BeginCount as Long
Dim EndCount2 as Long
Dim BeginCount2 as Long
Dim x as Long
Dim z as Long

LineFeed = ChrW(13)
InputFile = "c:.blah/blah.txt"
iFileNum = FreeFile
Open InputFile For Input As iFileNum
InputString = Input(LOF(iFileNum), iFileNum)
CurrentRecord = 0
CurrentFile
BeginCount = 1
For x = 1 To Len(InputString)
If Mid(InputString, x, 1) = LineFeed Then
EndCount = x – 1
CurrentFile = CurrentFile + 1
If EndCount > BeginCount Then
TempString = Mid(InputString,BeginCount, EndCount - BeginCount)
BeginCount2 = 1
For z = 1 to Len(TempString)
If Mid(TempString, z, 1) = “ “ Then
EndCount2 = z - 1
CurrentRecord = CurrentRecord + 1
If EndCount2 > BeginCount2 Then
TableData(CurrentFile,CurrentRecord)= Mid(TempString, BeginCount2, EndCount2 – BeginCount2)
End If
BeginCount2 = z + 1
End If
Next z
End If
BeginCount = x + 1
End If
Next x

Dec 29th, 1999, 12:28 AM
why use all that slow code when MS has given us the glorious "SPLIT" command:

assuming that the data values are separated by spaces:


Do Until EOF(FileNumber)
line Input #FileNumber, MyString
MyArray = Split(Mystring, " ")
'manipulate array before refreshing the data...
loop


so if the original line in the file was:

12 234 553 644 925

this means that:

MyArray(0) = 12
MyArray(1) = 234
MyArray(2) = 553 ...and so on

what you have to remember is to declare the array as a variant, and do not explicitly set the number of elements. Declare it like this:

Dim MyArray() as Variant

email me for more info.



------------------

Wossname,
Email me: wossnamex@talk21.com :)

exx_maverick
Dec 31st, 1999, 10:24 PM
Thanks for you help everyone, what you suggested worked and you have made my world a much happier place.

Mav