Geek Speak — HabitableZone

Geek Speak » in reply to re: Problem: Text-2-Binary conversion in Visual Basic

re: Visual Basic 6.0 code example

The text file was 99 percent numbers, essentially a database file with hundreds of thousands of records, each with a dozen fields. Just for descriptive purposes, suppose each record had a header with a name (string data type), a number representing an array size (double data type), followed by the array which contained a huge volume of numerical data (double data type). I used the following general code (simplified):

Option Explicit

Private Type cRecord
field1 as string
field2 as double
field3() as double
End Type

Private Sub Form_Activate()
dim oneRecord as cRecord

Open "filename.txt" for input as #1
input #1, oneRecord.field1
input #1, oneRecord.field2
redim oneRecord.field3(oneRecord.field2) as double
for k = 0 to oneRecord.field2
input #1, oneRecord.field3(k)
next k
Close #1

Open "fileout.bin" for binary access write as #2
put #2,, oneRecord
Close #2
End Sub

I think that's pretty much it. The above code would need a few more things to actually work, and of course a database text file, but the structure is the same. It created a binary file larger than the original text file, while all the literature, including the online MSDN library, tells me that binary files are supposed to conserve disk space and run faster! I've also been told that binary output is supposed to compress all the numbers, so I should have seen some serious reduction in size, since the file is mostly numbers.

So what gives?

Log in or register to post.

The whole thread (14 posts)