Hello everybody
I want my VB 5 Project to read a text file line by line to put each line in an array.
Thank you very much if you can help me.
Printable View
Hello everybody
I want my VB 5 Project to read a text file line by line to put each line in an array.
Thank you very much if you can help me.
Code:Private Sub Command1_Click()
Dim ar() As String
Dim i As Integer
i = 1
Open "c:\textfile.txt" For Input As #1
Do While Not EOF(1)
ReDim Preserve ar(0 To i) As String
Line Input #1, ar(i)
'ar contains lines
i = i + 1
Loop
Close #1
End Sub
0 is the default base, you could have just wroteCode:ReDim ar(i)
You just love posting to get your postcount up, don't you, Megatron.
Oops! I just did it too :D.
So, I better post some content.
You can also store files in a database-type storage, (one field though), and then retrieve/write using get/put.
why do you use "Preserve" when redimming the array?
Each time you execute the ReDim statement, all the values currently stored in the array are lost. Visual Basic resets the values to the Empty value (for Variant arrays), to zero (for numeric arrays), to a zero-length string (for string arrays), or to Nothing (for arrays of objects).
This is useful when you want to prepare the array for new data, or when you want to shrink the size of the array to take up minimal memory. Sometimes you may want to change the size of the array without losing the data in the array. You can do this by using ReDim with the Preserve keyword. For example, you can enlarge an array by one element without losing the values of the existing elements using the UBound function to refer to the upper bound:
ok, so it basically just keeps the value in the array.
thanks
No, I was correcting him on something.Quote:
You just love posting to get your postcount up, don't you, Megatron.
For simple file name storage, arrays are much cheaper than using a Database.Quote:
You can also store files in a database-type storage, (one field though), and then retrieve/write using get/put.