|
-
Dec 14th, 2006, 12:17 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Writing file taking to long....
I've got an array that is used to create a binary file. This file has a certain format to it (26 bytes per record) but for it to write 1068 items (size of array) takes 35 seconds??? If I create a file for Output and add the same values is takes 1 second???
I can't see why it takes so long to create the binary file? Can anyone help shed some light on this for me?
VB Code:
Public Sub CreateDataManFile(tMainData() As TypeGraphData)
Dim intFile As Integer
Dim DataManFile As String
Dim sString As String * 14
Dim i As Long
Dim j As Integer
Dim dateDay As Byte
Dim dateMonth As Byte
Dim dateYear As Integer
DataManFile = App.Path & "\Lintz.mmd"
'Check if dataman file exists and if so delete it
If Dir$(DataManFile) <> "" Then
Kill DataManFile
End If
intFile = FreeFile
Open DataManFile For Binary Access Write As #intFile
Debug.Print "Start Time Without Date = " & Now
For i = 0 To UBound(tMainData) '1068 items
Put #intFile, LOF(intFile) + 1, tMainData(i).fAmt
'This gets the day, month, year depending on users regional settings
Call GetCurrentDate(dateDay, dateMonth, dateYear, CDate(tMainData(i).dDate))
'fill in 18 bytes as data inbetween is not used
j = 5
Do Until j = 23
Put #intFile, LOF(intFile) + 1, CByte(0)
j = j + 1
Loop
'Add Day
Put #intFile, LOF(intFile) + 1, dateDay
'Add Month
Put #intFile, LOF(intFile) + 1, dateMonth
'Add Year
Put #intFile, LOF(intFile) + 1, dateYear
DoEvents
Next
Close #intFile
Debug.Print "End Time = " & Now '35 seconds
End Sub
-
Dec 14th, 2006, 12:52 AM
#2
Re: Writing file taking to long....
You can just dump the UDT to the file.
VB Code:
Dim udtType() As MYTYPE
Dim intFF As Integer
ReDim udtType(50) As MYTYPE
intFF = FreeFile
Open "C:\dump.bin" For Binary Access Write As #intFF
Put #intFF, 1, udtType()
Close #intFF
-
Dec 14th, 2006, 01:08 AM
#3
Re: Writing file taking to long....
Also, if you read the data back into the array using the Get #intff, 1, udtType(), you will need to know how many records are in the file so you can ReDim udtType() appropriately...
Try this. I can't test it right now but it should work...just put this code into an empty form:
VB Code:
Option Explicit
Private Type MYTYPE
strOne As String
strTwo As String
strThree As String
End Type
Private udtType() As MYTYPE
'Turns a number like "4" into "0004"
Private Function PadNumber(TheNumber As String, ByVal Length As Long) As String
Dim lonLen As Long
lonLen = Len(TheNumber)
If lonLen = Length Then
PadNumber = TheNumber
Else
PadNumber = String$(Length - lonLen, "0") & TheNumber
End If
End Function
'Turns a number like "0004" into "4"
Private Function RemoveHeader(TheHeader As String) As String
Dim lonLen As Long, lonLoop As Long
lonLen = Len(TheHeader)
For lonLoop = 1 To lonLen
If Not Mid$(TheHeader, lonLoop, 1) = "0" Then
RemoveHeader = Mid$(TheHeader, lonLoop)
Exit Function
End If
Next lonLoop
End Function
'Debug the array.
Private Sub DebugType()
Dim intLoop As Integer
Dim strName As String
For intLoop = 0 To UBound(udtType())
With udtType(intLoop)
strName = "udtType(" & intLoop & "):"
Debug.Print strName
Debug.Print String$(Len(strName), "-")
Debug.Print "One: " & .strOne
Debug.Print "Two: " & .strTwo
Debug.Print "Three: " & .strThree
Debug.Print ""
End With
Next intLoop
End Sub
'Just load some stuff into the UDT array.
Private Sub InitType()
Dim intLoop As Integer
Randomize
ReDim udtType(50) As MYTYPE
For intLoop = 0 To 50
With udtType(intLoop)
.strOne = "1 (" & intLoop & ")"
.strTwo = CStr(Int(Rnd * 255) + 1)
.strThree = CStr(Int(Rnd * 255) + 1)
End With
Next intLoop
End Sub
Private Sub Form_Load()
Dim intFF As Integer
Dim strHeader As String
'Load stuff into the array.
InitType
intFF = FreeFile
strHeader = PadNumber(UBound(udtType), 4)
'Write file.
Open "C:\dump.txt" For Binary Access Write As #intFF
'Write header.
Put #intFF, 1, strHeader
'Write data.
Put #intFF, Len(strHeader) + 1, udtType()
Close #intFF
DebugType
'Load file back.
intFF = FreeFile
strHeader = String$(4, " ")
Dim lonLenData As Long
Open "C:\dump.txt" For Binary Access Read As #intFF
'Read header.
Get #intFF, 1, strHeader
'Read data.
lonLenData = CLng(RemoveHeader(strHeader))
ReDim udtType(lonLenData) As MYTYPE
'Data always starts at 5th byte (header is 4 bytes).
Get #intFF, 5, udtType()
Close #intFF
DebugType
End Sub
-
Dec 14th, 2006, 05:37 AM
#4
Thread Starter
PowerPoster
Re: Writing file taking to long....
Thanks DigiRev, worked like a charm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|