Results 1 to 4 of 4

Thread: [RESOLVED] Writing file taking to long....

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved [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:
    1. Public Sub CreateDataManFile(tMainData() As TypeGraphData)
    2. Dim intFile As Integer
    3. Dim DataManFile As String
    4. Dim sString As String * 14
    5. Dim i As Long
    6. Dim j As Integer
    7. Dim dateDay As Byte
    8. Dim dateMonth As Byte
    9. Dim dateYear As Integer
    10. DataManFile = App.Path & "\Lintz.mmd"
    11.  
    12.     'Check if dataman file exists and if so delete it
    13.     If Dir$(DataManFile) <> "" Then
    14.     Kill DataManFile
    15.     End If
    16.  
    17. intFile = FreeFile
    18. Open DataManFile For Binary Access Write As #intFile
    19.        
    20. Debug.Print "Start Time Without Date = " & Now
    21.        
    22.     For i = 0 To UBound(tMainData) '1068 items
    23.    
    24.     Put #intFile, LOF(intFile) + 1, tMainData(i).fAmt
    25.    
    26.         'This gets the day, month, year depending on users regional settings
    27.     Call GetCurrentDate(dateDay, dateMonth, dateYear, CDate(tMainData(i).dDate))
    28.    
    29.         'fill in 18 bytes as data inbetween is not used
    30.         j = 5
    31.         Do Until j = 23
    32.         Put #intFile, LOF(intFile) + 1, CByte(0)
    33.         j = j + 1
    34.         Loop
    35.    
    36.         'Add Day
    37.         Put #intFile, LOF(intFile) + 1, dateDay
    38.        
    39.         'Add Month
    40.         Put #intFile, LOF(intFile) + 1, dateMonth
    41.        
    42.         'Add Year
    43.         Put #intFile, LOF(intFile) + 1, dateYear
    44.        
    45.     DoEvents
    46.     Next
    47.    
    48.     Close #intFile
    49. Debug.Print "End Time = " & Now '35 seconds
    50.    
    51. End Sub

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Writing file taking to long....

    You can just dump the UDT to the file.

    VB Code:
    1. Dim udtType() As MYTYPE
    2.     Dim intFF As Integer
    3.    
    4.     ReDim udtType(50) As MYTYPE
    5.    
    6.     intFF = FreeFile
    7.    
    8.     Open "C:\dump.bin" For Binary Access Write As #intFF
    9.         Put #intFF, 1, udtType()
    10.     Close #intFF

  3. #3
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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:
    1. Option Explicit
    2.  
    3. Private Type MYTYPE
    4.     strOne As String
    5.     strTwo As String
    6.     strThree As String
    7. End Type
    8.  
    9. Private udtType() As MYTYPE
    10.  
    11. 'Turns a number like "4" into "0004"
    12. Private Function PadNumber(TheNumber As String, ByVal Length As Long) As String
    13.     Dim lonLen As Long
    14.    
    15.     lonLen = Len(TheNumber)
    16.    
    17.     If lonLen = Length Then
    18.         PadNumber = TheNumber
    19.     Else
    20.         PadNumber = String$(Length - lonLen, "0") & TheNumber
    21.     End If
    22.    
    23. End Function
    24.  
    25. 'Turns a number like "0004" into "4"
    26. Private Function RemoveHeader(TheHeader As String) As String
    27.     Dim lonLen As Long, lonLoop As Long
    28.    
    29.     lonLen = Len(TheHeader)
    30.    
    31.     For lonLoop = 1 To lonLen
    32.        
    33.         If Not Mid$(TheHeader, lonLoop, 1) = "0" Then
    34.             RemoveHeader = Mid$(TheHeader, lonLoop)
    35.             Exit Function
    36.         End If
    37.    
    38.     Next lonLoop
    39.    
    40. End Function
    41.  
    42. 'Debug the array.
    43. Private Sub DebugType()
    44.     Dim intLoop As Integer
    45.     Dim strName As String
    46.    
    47.     For intLoop = 0 To UBound(udtType())
    48.        
    49.         With udtType(intLoop)
    50.             strName = "udtType(" & intLoop & "):"
    51.             Debug.Print strName
    52.             Debug.Print String$(Len(strName), "-")
    53.             Debug.Print "One:   " & .strOne
    54.             Debug.Print "Two:   " & .strTwo
    55.             Debug.Print "Three: " & .strThree
    56.             Debug.Print ""
    57.         End With
    58.    
    59.     Next intLoop
    60.    
    61. End Sub
    62.  
    63. 'Just load some stuff into the UDT array.
    64. Private Sub InitType()
    65.     Dim intLoop As Integer
    66.    
    67.     Randomize
    68.    
    69.     ReDim udtType(50) As MYTYPE
    70.    
    71.     For intLoop = 0 To 50
    72.        
    73.         With udtType(intLoop)
    74.             .strOne = "1 (" & intLoop & ")"
    75.             .strTwo = CStr(Int(Rnd * 255) + 1)
    76.             .strThree = CStr(Int(Rnd * 255) + 1)
    77.         End With
    78.    
    79.     Next intLoop
    80.    
    81. End Sub
    82.  
    83. Private Sub Form_Load()
    84.     Dim intFF As Integer
    85.     Dim strHeader As String
    86.    
    87.     'Load stuff into the array.
    88.     InitType
    89.    
    90.     intFF = FreeFile
    91.     strHeader = PadNumber(UBound(udtType), 4)
    92.    
    93.     'Write file.
    94.     Open "C:\dump.txt" For Binary Access Write As #intFF
    95.         'Write header.
    96.         Put #intFF, 1, strHeader
    97.         'Write data.
    98.         Put #intFF, Len(strHeader) + 1, udtType()
    99.     Close #intFF
    100.    
    101.     DebugType
    102.    
    103.     'Load file back.
    104.     intFF = FreeFile
    105.     strHeader = String$(4, " ")
    106.    
    107.     Dim lonLenData As Long
    108.    
    109.     Open "C:\dump.txt" For Binary Access Read As #intFF
    110.         'Read header.
    111.         Get #intFF, 1, strHeader
    112.        
    113.         'Read data.
    114.         lonLenData = CLng(RemoveHeader(strHeader))
    115.         ReDim udtType(lonLenData) As MYTYPE
    116.        
    117.         'Data always starts at 5th byte (header is 4 bytes).
    118.         Get #intFF, 5, udtType()
    119.     Close #intFF
    120.    
    121.     DebugType
    122. End Sub

  4. #4

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Resolved 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
  •  



Click Here to Expand Forum to Full Width