Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

almost got string into 2 dimensional array need a little help 1

Status
Not open for further replies.

visualJay

Programmer
Jun 14, 2001
57
US
Here is all my code for this section

Dim reportArray() As String
Dim reportstring(0, 4) As String
Dim Report As String
Dim cnt As Integer
Dim i As Integer
Dim x As Integer
cnt = 0
Open "file.txt" For Input As #4

x = 0
Do Until (EOF(4))
Line Input #4, iCopyString
reportArray = Split(iCopyString, ",", -1)
For i = 0 To 4

****below is error array already dimensioned****
reportstring(x, i) = reportArray(i)

Next
cnt = cnt + 1
ReDim reportstring(x + 1, 4)
x = x + 1
DoEvents
Loop

Close #4
**** This is just to show me its working *****

Load frmError
iCopyString = ""
For i = 0 To cnt
For x = 0 To 4
iCopyString = iCopyString & reportstring(i, x) & " "
Next
iCopyString = iCopyString & vbCrLf
Next
frmError.Text1.Text = iCopyString

I am trying to move a csv file into a two dimensional array so that I can take the parts of the array I need along with other info and write it to a new file. Problem is I do not know the number of lines in the csv file (it changes daily)
can any help

 
If I understand you correctly, you want to parse out the elements of a csv file into a 2-d array. Here is some general stuff:

dim fil as integer
dim fName as string
dim upper as long, i as long,j as long 'indices
dim rawFile as string 'Holds the entire contents of the file
dim rawSplit() as string 'the file split up on a line per line basis
dim oneLine() as string 'used to hold the tokens for one line
dim reportArray() as string ' the 2d array that holds X lines and 0 to 4 elements per line

fil = FreeFile 'get the next free file number
fName = "file.txt"

'open the file and dump the contents into the rawfile variable

Open fName For Binary As #fFile
rawFile = String$(LOF(fFile), 32)
Get #fFile, 1, rawFile
Close #fFile
'A Note on the above method, it works for large files. I tried it with a 50 meg file on a computer with 128 Mb of ram and it worked file.

rawSplit = Split(rawFile, vbCrLf) 'split the file up by lines

upper = ubound(rawSplit) 'the number of lines in the file
redim reportArray(0 to upper, 0 to 4)


for i = 0 to upper
oneline = split(rawsplit(i),",")
for j = 0 to 4
reportArray(i,j) = oneline(j)
next j
next i


Good Luck, Hope this helps....


Troy Williams B.Eng.
fenris@hotmail.com

 
I (together with others) posted a solution based on the FileSystemObject that may fit your requirements in thread222-51020 _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top