Here's something I put together last night and modified a bit this morning. Basically this is, in my opinion, the best way of doing file I/O in VB using native VB functions.
My code is the third method.
Its fast, but its also in a loop, so that the application won't look crashed.
Let me know what you think ...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
i'm guessing that the usual functions, and string appending basically copies memory accross at some point (it would have to!), but your method skips the error checking etc. so its quicker.
i wonder what the downsides are?
if its dangerous to use?
If it's not then i'll use it all the time
be worth finding out...?
Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."
oooo a slapping. I like the idea of that
Heh, have you seen the Sentience thread in the Games&Graphics Programming forum ?
About 35 pages full of ****e
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
As mentioned before: GET is REALLY faster.
My code looks like this (I do not use the Space function, Rather the String function)
Code:
'' "Clear" the Immediate Window
''
Dim i As Long
For i = 0 To 50
Debug.Print
Next
'' A few declarations outside of the timing loops
''
Dim myFile As String: myFile = "c:\plenderjTestFile.txt"
Dim myLof As Long, time1 As Long, time2 As Long, time3 As Long, time4 As Long
Dim strBuff As String, strBuff2 As String, strBuff3 As String, strBuff4 As String, strBuff5 As String, currPos As Long
'' Create a test file to work with
''
Debug.Print "1/6) Creating test file ..."
Open myFile For Output As #1
For i = 0 To 50000
Print #1, i
Next
Close #1
Debug.Print "2/6) Created! , File length : " & FileLen(myFile)
myLof = FileLen(myFile)
'' First test. Input entire length of file into memory using Input()
''
Debug.Print "3/6) Input(Lof(), #fileNumber)"
time1 = GetTickCount
Open myFile For Binary As #1
strBuff = Input(myLof, 1)
Close #1
time1 = GetTickCount - time1
'' Second test. Read in and append 4096 blocks to a buffer using a Do...Loop
''
Debug.Print "4/6) Now using normal Do...Loop"
time2 = GetTickCount
Open myFile For Binary As #1
Do Until Loc(1) = myLof
strBuff2 = strBuff2 & Input(4096, 1)
Loop
Close #1
time2 = GetTickCount - time2
'' Third test. Read in and append 4096 blocks to a buffer using a Do...Loop and special code for appending
''
Debug.Print "5/6) Now using modified Do...Loop"
time3 = GetTickCount
strBuff4 = Space(myLof)
Open myFile For Binary As #1
Do Until Loc(1) = myLof
strBuff3 = Input(4096, 1)
CopyMemory ByVal StrPtr(strBuff4) + currPos, ByVal StrPtr(strBuff3), LenB(strBuff3)
currPos = currPos + LenB(strBuff3)
Loop
Close #1
time3 = GetTickCount - time3
'' Fourth Test
Debug.Print "6/6) Now using GET, NO LOOP"
time4 = GetTickCount
Open myFile For Binary As #1
strBuff5 = String(myLof, vbNull)
Get #1, , strBuff5
Close #1
time4 = GetTickCount - time4
'' Display results, and confirm that resulting file buffers are equal
''
MsgBox "Code times : " & vbCrLf & _
" Input(Lof(), #filenum)" & vbTab & time1 & vbCrLf & _
" Normal Do...Loop " & vbTab & time2 & vbCrLf & _
" Modified Do...Loop " & vbTab & time3 & vbCrLf & _
" And when using GET" & vbTab & time4 & vbCrLf & vbCrLf & vbCrLf & _
" Buffer sizes : " & vbTab & Len(strBuff) & "," & Len(strBuff2) & "," & Len(strBuff4) & "," & Len(strBuff5) & vbCrLf & _
" Buffers equal : " & vbTab & ((strBuff2 = strBuff4) And (strBuff = strBuff2) And (strBuff = strBuff5))
End