Results 1 to 7 of 7

Thread: Store and output a binary file

  1. #1

    Thread Starter
    Lively Member stever2003's Avatar
    Join Date
    Dec 2000
    Posts
    109

    Store and output a binary file

    How can I store a binary file in my program, and output it to a file later?

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Create a resource file. Start the Resource Editor from the tools menu (if you don't have it you have to first goto Add-Ins|Add-In Manager and start the VB6 Resource Editor add-in.).
    Click the "Add Custom Resource..." button on the toolbar and browse to the file you want to add.
    The resource is added with a new ID (probably 101) under a "CUSTOM" folder.

    To write it back to disk use code simular to this:
    Code:
    Private Sub SaveIt(ByVal FileName As String)
        Dim bArr() As Byte
        Dim hFile As Integer
        
        bArr = LoadResData(101, "CUSTOM")
        hFile = FreeFile
        Open FileName For Binary Access Write As #hFile
        Put #hFile, , bArr
        Close #hFile
    End Sub
    Best regards

    PS. Funny avitar your using! You had me fooled for a second.

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    is this you mean?

    VB Code:
    1. Dim buff As String
    2.  
    3.     Open "C:\Crsyb14.hlp" For Binary As #1
    4.         buff = String(LOF(1), Chr(0))
    5.         Get #1, 1, buff
    6.     Close #1
    7.    
    8.     Open "C:\Crsyb15.hlp" For Binary As #1
    9.         Put #1, 1, buff
    10.     Close #1

    regards,

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by Chris
    is this you mean?

    VB Code:
    1. Dim buff As String
    2.  
    3.     Open "C:\Crsyb14.hlp" For Binary As #1
    4.         buff = String(LOF(1), Chr(0))
    5.         Get #1, 1, buff
    6.     Close #1
    7.    
    8.     Open "C:\Crsyb15.hlp" For Binary As #1
    9.         Put #1, 1, buff
    10.     Close #1

    regards,
    The above will only read and write one byte. You shouldn't use strings either because they store two bytes for every character. Use a byte array instead.

  5. #5
    chenko
    Guest
    That code worked for me.

  6. #6

    Thread Starter
    Lively Member stever2003's Avatar
    Join Date
    Dec 2000
    Posts
    109
    hi, i just got back from a trip so i haven't tried out any of your code samples yet. thanks for replying. i'm just wondering if it is possible to store a byte array permanently in a program. Instead of loading it from a file every time I run a program, is it possible for it to be inside the program already? I'm asking this because if I give a program to other people, instead of sending them an executable and other data files, I just want to send them one file. again, thanks for helpinh.

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Yes you can. Simply add them to a Resource File.

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