|
-
Jul 2nd, 2001, 05:19 PM
#1
Thread Starter
Member
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...
-
Jul 2nd, 2001, 09:24 PM
#2
You don't need to use the API, here's a perfectly good method for open large files using standard VB commands:
VB Code:
Public Function OpenFile(ByVal sFilename As String) As String
Dim iFile As Integer
Dim sContent As String
Dim sChunk As String
Dim lChunkSize As Long
' If the file doesn't exist, return an empty string.
If Len(Dir(sFilename)) = 0 Then Exit Function
lChunkSize = 64000 ' Default Chunk Size (64K)
' Get the next available file
iFile = FreeFile
' Open the File for Binary Access
Open sFilename For Binary Access Read As iFile
' Extract the contents of the file until we get it all
Do While Loc(iFile) < LOF(iFile)
If lChunkSize + Loc(iFile) > LOF(iFile) Then lChunkSize = LOF(iFile) - Loc(iFile)
sChunk = Space(lChunkSize)
Get #iFile, , sChunk
sContent = sContent & sChunk
Loop
Close iFile
' Return the contents of the file in a string
OpenFile = sContent
End Function
Public Function SaveFile(ByVal sFilename As String, ByVal sContent As String) As Boolean
Dim iFile As Integer
iFile = FreeFile
Open sFilename For Binary Access Write As iFile
Put #iFile, , sContent
Close iFile
End Function
Example Usage:
VB Code:
sFileText = OpenFile("C:\SomeFile.txt")
You should also be aware though, that there is a 32K limit on the Textbox control.
-
Jul 2nd, 2001, 11:43 PM
#3
Registered User
Due to the limitations of the textbox, you could try a rich text box:
VB Code:
Private Sub Form_Load()
Me.RichTextBox1.LoadFile ("C:\tmp\test.txt")
End Sub
-
Jul 3rd, 2001, 10:46 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|