Results 1 to 26 of 26

Thread: Open a file in memory then destroy.

  1. #1

    Thread Starter
    Addicted Member Chrispybee's Avatar
    Join Date
    Sep 2003
    Location
    North Wales, UK
    Posts
    217

    Open a file in memory then destroy.

    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?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim x As Integer, st As String
    5.   Dim ff As Integer
    6.   Dim strBuff As String
    7.   Dim str() As String
    8.   ff = FreeFile
    9.   Open App.Path & "\to do.txt" For Input As #ff
    10.     strBuff = Input(LOF(ff), ff)
    11.   Close #ff
    12.   ' ----------------- two ways to skin a cat --------------
    13.   MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
    14.   ' -------------------------------------------------------
    15.   str() = Split(strBuff, vbCrLf)
    16.   MsgBox "There are " & UBound(str) + 1 & " lines in the file"
    17.   For x = 0 To UBound(str)
    18.     st = st & str(x) & vbCrLf
    19.   Next x
    20.   MsgBox st
    21. End Sub

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Open a file in memory then destroy.

    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:
    1. Option Explicit
    2. 'Add a CommonDialog Control to your toolbox and add to your form.
    3. 'Project > Components > Controls tab > select "MS Common Dialog Control" > click OK.
    4. Private arArray() As String
    5.  
    6. Private Sub Command1_Click()
    7.  
    8.     On Error GoTo My_Error
    9.    
    10.     Dim strFilePath As String
    11.     Dim strBuff As String
    12.    
    13.     With CommonDialog1
    14.         .CancelError = True
    15.         .DialogTitle = "Select a file"
    16.         .InitDir = App.Path
    17.         .ShowOpen
    18.         strFilePath = .FileTitle
    19.     End With
    20.    
    21.     Open strFilePath For Input As #1
    22.       strBuff = Input(LOF(1), 1)
    23.     Close #1
    24.    
    25.     arArray() = Split(strBuff, vbNewLine)
    26.     'File contents remail in the array in memory until erased
    27.     If MsgBox("Do you want to clear the array contents?", vbYesNo + vbQuestion) = vbYes Then
    28.         Erase arArray
    29.     End If
    30.     Exit Sub
    31.    
    32. My_Error:
    33.     If Err.Number = cdlCancel Then
    34.         MsgBox "File Open Canceled", vbOKOnly + vbExclamation
    35.     Else
    36.         MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
    37.     End If
    38. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Addicted Member Chrispybee's Avatar
    Join Date
    Sep 2003
    Location
    North Wales, UK
    Posts
    217

    Re: Open a file in memory then destroy.

    Hi RobDog888 - This is more like what I wanted. One question tho. How can I effectivly clear the
    VB Code:
    1. 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..

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Open a file in memory then destroy.

    strbuff = "" at end of procedure is the variable is local to the procedure

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    When you unload your form, all objects are destroyed, anyways.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Open a file in memory then destroy.

    Not always, but in this case, yes when the strBuff variable goes out of scope its cleared.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Addicted Member Chrispybee's Avatar
    Join Date
    Sep 2003
    Location
    North Wales, UK
    Posts
    217

    Re: Open a file in memory then destroy.

    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???

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    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

  10. #10
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    dglienna,

    What kind of problems?

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    With large files, I've had it crash while reading into the buffer.

  12. #12
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    dglienna,

    What code are you using for that?

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    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.

  14. #14
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    dglienna,

    Yeah, That Input statement probably does it. Try this:
    VB Code:
    1. Private Function ReadFile(Filename As String, Contents() As Byte)
    2. Dim FNum As Integer
    3.  
    4.     FNum = FreeFile
    5.     On Local Error GoTo ErrorRtn
    6.     Open Filename For Binary As #FNum
    7.     If LOF(FNum) = 0 Then Close #FNum: Exit Function
    8.     ReDim Contents(LOF(FNum))
    9.     Get #FNum, , Contents(): Close #FNum
    10.    
    11. ExitRtn:
    12.  
    13.     Exit Function
    14.    
    15. ErrorRtn:
    16.  
    17.     MsgBox Err.Number & " - " & Err.Description, vbCritical, App.Title
    18.     Resume ExitRtn
    19.    
    20. End Function

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    I read in 83 bytes. How do I change it back into text to display it?
    Last edited by dglienna; Sep 30th, 2005 at 07:34 PM.

  16. #16
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    dglienna,

    Do a Join to convert to a string.

  17. #17
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    dglienna,

    83 bytes? Boy thats a large file.

  18. #18
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    It's the principle. Just testing. Why doesn't this work?

    VB Code:
    1. Private Sub Command1_Click()
    2.   Dim str() As Byte, out$
    3.   out = ReadFile(App.Path & "\to do.txt", str())
    4.   out = Join(str())
    5.   MsgBox out
    6. End Sub

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    Can't figure it out...

  20. #20
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    dglienna,

    Sorry, Try this. It works. I forgot Join only works on string arrays.
    Attached Files Attached Files

  21. #21
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    dglienna,

    But be aware that the ReadFile routine does not do Tab conversions.

  22. #22
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    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.

  23. #23
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    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.
    Last edited by randem; Sep 30th, 2005 at 09:17 PM.

  24. #24
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    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.

  25. #25
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a file in memory then destroy.

    dglienna,

    Here's one that returns a string array.
    Attached Files Attached Files

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Open a file in memory then destroy.

    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.

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