Results 1 to 4 of 4

Thread: Faster File I/O with API?

  1. #1

    Thread Starter
    Member sTyLeZ's Avatar
    Join Date
    Jan 2001
    Posts
    35

    Question Faster File I/O with API?

    I'm writing a notepad-type application that will support files larger than the notepad limit, and I'm having trouble. It actually runs out of memory before it can even finish reading a file as small as 68KB .

    Anyone have any tips or pointers on how to (quickly [if possible]) open a file of any size (or at least files up to 1 MB)? Is there any API that I could use?

    My current method of retrieving the file contents is this:

    Code:
    Private Sub mnuOpen_Click()
    
    On Error GoTo Cancel:
    UpdateStatus "Open a file..."
    CommonDialog1.FLAGS = cdlOFNLongNames
    CommonDialog1.DialogTitle = "Advanced Text Editor - Open File"
    CommonDialog1.ShowOpen
    X$ = CommonDialog1.FileName
    
    '
    '
    'this is what actually inputs the file
    '
    '
    Open X$ For Binary As #1
    Text1.Text = ""
    L = LOF(1)
    Text1.Text = Input(L, 1)
    
    
    '
    'everything else is just junk
    '
    P = 1
    lastpos = 1
    While P <> 0
        P = InStr(lastpos + 1, X$, "\")
        If P <> 0 Then lastpos = P
    Wend
    thefile$ = Right(X$, Len(X$) - lastpos)
    ThePath$ = Left(X$, lastpos - 1)
    UpdateTitle thefile$
    UpdateStatus "File loaded successfully"
    Changed = False
    Cancel:
    Close
    If Err = 7 Then
        MsgBox "Out of memory", vbCritical, "Error"
        UpdateStatus "Error loading file: Out of memory"
    ElseIf Err <> 0 Then
        UpdateStatus "File not opened. Reason: " & Error(Err)
    End If
    
    End Sub
    Now the world is gone I'm just one...

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You don't need to use the API, here's a perfectly good method for open large files using standard VB commands:
    VB Code:
    1. Public Function OpenFile(ByVal sFilename As String) As String
    2.     Dim iFile As Integer
    3.     Dim sContent As String
    4.     Dim sChunk As String
    5.     Dim lChunkSize As Long
    6.    
    7.     ' If the file doesn't exist, return an empty string.
    8.     If Len(Dir(sFilename)) = 0 Then Exit Function
    9.    
    10.     lChunkSize = 64000  ' Default Chunk Size (64K)
    11.     ' Get the next available file
    12.     iFile = FreeFile
    13.     ' Open the File for Binary Access
    14.     Open sFilename For Binary Access Read As iFile
    15.     ' Extract the contents of the file until we get it all
    16.     Do While Loc(iFile) < LOF(iFile)
    17.         If lChunkSize + Loc(iFile) > LOF(iFile) Then lChunkSize = LOF(iFile) - Loc(iFile)
    18.         sChunk = Space(lChunkSize)
    19.         Get #iFile, , sChunk
    20.         sContent = sContent & sChunk
    21.     Loop
    22.     Close iFile
    23.    
    24.     ' Return the contents of the file in a string
    25.     OpenFile = sContent
    26. End Function
    27.  
    28. Public Function SaveFile(ByVal sFilename As String, ByVal sContent As String) As Boolean
    29.     Dim iFile As Integer
    30.    
    31.     iFile = FreeFile
    32.     Open sFilename For Binary Access Write As iFile
    33.     Put #iFile, , sContent
    34.     Close iFile
    35. End Function
    Example Usage:
    VB Code:
    1. sFileText = OpenFile("C:\SomeFile.txt")
    You should also be aware though, that there is a 32K limit on the Textbox control.

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Due to the limitations of the textbox, you could try a rich text box:
    VB Code:
    1. Private Sub Form_Load()
    2. Me.RichTextBox1.LoadFile ("C:\tmp\test.txt")
    3. End Sub

  4. #4
    Megatron
    Guest
    Make sure to specify the rtfText flag to open in text mode.

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