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).
Can this be done in VB6?
Printable View
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).
Can this be done in VB6?
Not sure that I understand, but you can read into a buffer. This is for a text file, and you can do similar things with a binary file.
VB Code:
Option Explicit Private Sub Form_Load() Dim x As Integer, st As String Dim ff As Integer Dim strBuff As String Dim str() As String ff = FreeFile Open App.Path & "\to do.txt" For Input As #ff strBuff = Input(LOF(ff), ff) Close #ff ' ----------------- two ways to skin a cat -------------- MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1 ' ------------------------------------------------------- str() = Split(strBuff, vbCrLf) MsgBox "There are " & UBound(str) + 1 & " lines in the file" For x = 0 To UBound(str) st = st & str(x) & vbCrLf Next x MsgBox st End Sub
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 Else MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation End If End Sub
Hi RobDog888 - This is more like what I wanted. One question tho. How can I effectivly clear thefrom being stored after the file has been opened and the array has been generated? I hope you get what I'm on about???VB Code:
strBuff
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..
strbuff = "" at end of procedure is the variable is local to the procedure
When you unload your form, all objects are destroyed, anyways.
Not always, but in this case, yes when the strBuff variable goes out of scope its cleared. :)
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.
Does anyone know if this is possible???
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
dglienna,
What kind of problems?
With large files, I've had it crash while reading into the buffer.
dglienna,
What code are you using for that?
The code that I posted. I was reading in html dictionary files by letter, and had to read line by line for a couple of the letters.
dglienna,
Yeah, That Input statement probably does it. Try this:
VB Code:
Private Function ReadFile(Filename As String, Contents() As Byte) Dim FNum As Integer FNum = FreeFile On Local Error GoTo ErrorRtn Open Filename For Binary As #FNum If LOF(FNum) = 0 Then Close #FNum: Exit Function ReDim Contents(LOF(FNum)) Get #FNum, , Contents(): Close #FNum ExitRtn: Exit Function ErrorRtn: MsgBox Err.Number & " - " & Err.Description, vbCritical, App.Title Resume ExitRtn End Function
I read in 83 bytes. How do I change it back into text to display it?
dglienna,
Do a Join to convert to a string.
dglienna,
83 bytes? Boy thats a large file.
It's the principle. Just testing. Why doesn't this work?
VB Code:
Private Sub Command1_Click() Dim str() As Byte, out$ out = ReadFile(App.Path & "\to do.txt", str()) out = Join(str()) MsgBox out End Sub
Can't figure it out...
dglienna,
Sorry, Try this. It works. I forgot Join only works on string arrays.
dglienna,
But be aware that the ReadFile routine does not do Tab conversions.
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.
dglienna,
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.
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.
dglienna,
Here's one that returns a string array.
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. :)