I would like to have the ability to open a file (any file, any size) and put it into memory (maybe allocated at an address?), Get the information from memory then destroy it from memory (free up memory).
This is exactly what you want. It will allow you to choose a file and read it into an array in memory. Then optionall clear the array contents. The array is accessible from any procedure in your form for other use.
VB Code:
Option Explicit
'Add a CommonDialog Control to your toolbox and add to your form.
'Project > Components > Controls tab > select "MS Common Dialog Control" > click OK.
Private arArray() As String
Private Sub Command1_Click()
On Error GoTo My_Error
Dim strFilePath As String
Dim strBuff As String
With CommonDialog1
.CancelError = True
.DialogTitle = "Select a file"
.InitDir = App.Path
.ShowOpen
strFilePath = .FileTitle
End With
Open strFilePath For Input As #1
strBuff = Input(LOF(1), 1)
Close #1
arArray() = Split(strBuff, vbNewLine)
'File contents remail in the array in memory until erased
If MsgBox("Do you want to clear the array contents?", vbYesNo + vbQuestion) = vbYes Then
Erase arArray
End If
Exit Sub
My_Error:
If Err.Number = cdlCancel Then
MsgBox "File Open Canceled", vbOKOnly + vbExclamation
Hi RobDog888 - This is more like what I wanted. One question tho. How can I effectivly clear the
VB Code:
strBuff
from being stored after the file has been opened and the array has been generated? I hope you get what I'm on about???
Basically, I want to make sure that at the end of my applications, there's absolutly nothing being resident in memory when I've finished with Arrays, strings and all other variables..
Cheers guys. I just want to make my applications as efficient as possible.
I would like to get to this stage:
When you run your application, it will take around 1.5mb. You do some stuff (loading files, saving and displaying lots of forms and strings, arrays). Once this has been done, I would like to get my app down to the 1.5mb memory usage.
I've run into problems trying to read large files into a buffer, and had to resort to reading in line by line. If you read into an array, you can still erase it when you are done with it
Thanks. It was only a simple test, just to clarify what was needed. I see you used CopyMemory. All other samples I saw read directly into a string, which is why I was confused.
Strings are limited to a max size (65535 I believe), however byte arrays are limited by available memory (can be virtually unlimited). So, converting to a string is not a very good idea. Very large text files should be put into a string array and processed that way. Non text files should be left in the byte array.
Last edited by randem; Sep 30th, 2005 at 09:17 PM.
I might have to use your sample in the future, unless I modify it a little more than I already did for my test. I see no problem with parsing the string after it's read into a byte array. Plus, you can process in chunks.
That really helped. I'll have to look at it tomorrow to study it, as it's getting late. Thanks. There appears to be a method to the madness of binary files.