-
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
-
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
-
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
-
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
-
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.
-
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
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks