Saving data in a binary file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Saving data in a binary file

  1. #1
    Join Date
    Mar 2005
    Posts
    49

    Saving data in a binary file

    Hello,

    I have 3 data arrays which i am saving in an Access database. But, I found out that saving them in a database is not a very good idea in terms of memory usage. I came to know that I could save the data in a binary file and could retrieve it whenever i need it. can anyone please tell me how I can save data in a data array on to a binary file and retrieve it when requied?

    Thanks

  2. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    you can use the basic VB file I/O Open, Get, Put and Close.
    This is just an example on how to read/write an array of bytes.
    You can just use any data you want, the important is that when you read you use the exact format used for writing. In binary files, usually the beginning is an header (defined as a UDT) that explains offset and length in the file where the data is.
    Code:
        '' write an array
        Dim ff As Integer
        Dim data() As Byte
        ReDim data(1023)
        ff = FreeFile
        Open "C:\tmp.data" For Binary Access Write As #ff
        Put #ff, , data
        Close #ff
        
        '' read an array
        ff = FreeFile
        Open "C:\tmp.data" For Binary Access Read As #ff
        ReDim data(LOF(ff) - 1)
        Get #ff, , data
        Close #ff
    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  3. #3
    Join Date
    Mar 2005
    Posts
    49
    Hi mstraf,

    Thanks for your reply. I tried writing a data array into a binary file. but when i opened the saved file, I dont see any data in it. I modified your code to save a data array and is as shown below. Please help me out in find the error.

    Code:
    Private A() As Single
    
    Private Sub Command1_Click()
        '' write an array
        Dim ff As Integer
        'Dim data() As Byte
        ReDim A(100)
        ff = FreeFile
        Open "C:\tmp1.data" For Binary Access Write As #ff
        Put #ff, , A
        Close #ff
    End Sub
    
    Private Sub Command2_Click()
        '' read an array
        ff = FreeFile
        Open "C:\tmp1.data" For Binary Access Read As #ff
        ReDim A(LOF(ff) - 1)
        Get #ff, , A
        Close #ff
    End Sub
    
    Private Sub Form_Load()
    ReDim Preserve A(100)
    For i = 1 To 100
        A(i) = i
    Next i
    End Sub

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    you are redim-ming the A array before saving it, thus losing the data saved in the Load event
    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  5. #5
    Join Date
    Mar 2005
    Posts
    49
    But I am using the "Preserve" keyword.....

    The MSDN documentation says...

    "The following statement resizes the array but does not erase elements.

    Redim Preserve MyArray(15) ' Resize to 15 elements."


    so i dont think thats the problem.

  6. #6
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    you are using preserve in the Load event (that is useless, because at that time A was not initialized yet) and NOT afterwards (in the command1_click) you must always use it!
    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  7. #7
    Join Date
    Mar 2005
    Posts
    49
    Yup, got it. Thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center