com >> Binary File I/O with variable string member

by VJ » Thu, 22 Dec 2005 00:00:26 GMT

Hi,

I am working on a small application and dont want to store records in any
database (access,sql. etc). I want to write & read

information from my own binary file, for this purpose I have declared type
in my application which is as follows.

Public Type CustDetails
CustID as long
FName as string
LName as string
Addr as string
City as string
State as string
Country as string
DateofBirth as date
Weight as double
.
.
.
...etc. etc.
End Type


Now to save disk space I dont want to specify the length of string type
memebers.

I have declared the customer array and can save that customer array into
file easily but problem comes while loading this file back to the customer
array as I dont know what exactly would be the number of records saved in
this file so that i can redim my array accordingly.

My code for saving and retreving type array as follows


Dim arrCustomer() As CustDetails
Dim fso As New FileSystemObject
Private Const mFileName As String = "Customers.dat"

Private Sub SaveFile()
If GetUBound(arrCustomer) < 0 Then
Exit Sub
End If

Dim arrCounter As Integer
Dim FileNum As Integer
Dim DataFilePath As String
DataFilePath = App.Path & "\" & mFileName
FileNum = FreeFile
Open App.Path & "\" & mFileName For Binary As FileNum

Put FileNum, , arrCustomer
Close (FileNum)

MsgBox "Saved..", vbInformation
End Sub


Public Sub LoadFile()
Dim FileNum As Integer, RecordNumber As Integer
Dim DataFilePath As String

DataFilePath = App.Path & "\" & mFileName
If fso.FileExists(DataFilePath) Then
FileNum = FreeFile
Open App.Path & "\" & mFileName For Binary As FileNum

ReDim arrCustomer(0) ' this is a problem

Get FileNum, , arrCustomer
Close (FileNum)
End If
End Sub


Please help as I beleive there should be some way of retreiving variable
type member records. I have searched on the net but couldnt find out the
solution for this problem


Best regards
VJ




com >> Binary File I/O with variable string member

by Bob Butler » Thu, 22 Dec 2005 00:24:03 GMT






Put #FileNum,, clng(ubound(arrCustomer)) ' record count


Make that: Close #FileNum
VB does not use () when you don't use CALL


Dim RecordCount As Long


Get #FileName,,RecordCount
ReDim arrCustomer(RecordCount) ' this is a problem


Close #FileNum


--
Reply to the group so all can participate
VB.Net: "Fool me once..."




com >> Binary File I/O with variable string member

by Ulrich Korndoerfer » Thu, 22 Dec 2005 02:11:23 GMT

Hi,

VJ schrieb:

Private Sub SaveFile(ByRef theArray As Variant)

Put FileNum, , theArray
Public Function LoadFile() As CustDetails()
Dim theArray As Variant
Delete line above
Get FileNum, , theArray
LoadFile = theArray

Call it such: arrCustomer = LoadFile

--
Ulrich Korndoerfer

VB tips, helpers, solutions -> http://www.proSource.de/Downloads/

-----------------------------------------------------------------------
Plea for a bright future for VB classic.
Sign the petition to Microsoft: -> http://classicvb.org/petition/
-----------------------------------------------------------------------


Binary File I/O with variable string member

by Jeff Johnson [MVP: VB] » Thu, 22 Dec 2005 03:06:46 GMT






...and while you might say "it works fine for me," yes, in this instance it
does, but someday it'll bite you in the ass, so just avoid it and do it the
way Bob showed.




Binary File I/O with variable string member

by erewhon » Thu, 22 Dec 2005 18:40:21 GMT





While I wholeheartedly agree with you in doing your own filing, your
current approach is a bit ... fragile.

If anything goes wrong with one bit of your file, it will shaft the
whole thing and be very hard to fix.

You would be a lot better off storing the data in plain text that can
be inspected and modified with a simple text editor

Something like the INI file format will do the trick

While it looks as if the processor is doing a lot more work, the work
is taking place in memory - and that is extremely fast

The advantages of being able to manually inspect your data and
physically fix/modify it are well worth considering.

It would also be an idea to 'wrap' the whole lot in a Class, which
will allow you to change/update your strategy without having to modify
Application code.




Binary File I/O with variable string member

by Ralph » Thu, 22 Dec 2005 21:22:40 GMT





type

I agree with this.

It is also useful to borrow a page from .dbf files (arguably the most
successful data file format after diff files ever invented) and include a
header providing at a minimum version information, and perhaps field order
and type.

Mr French's advise concerning flexibility is particularly germaine to the
example you gave. I was with you (something light-weight) until I saw
"country" and the absence of any "Postal Code" or other legal political
divisions - what about mutliple line 'addresses'? Weight measured in what?
Date format? ...

Second I would be concerned that whenever one goes about creating a bag of
loosely related 'attributes' they are destined to create a data-centric
application with all the pitfalls such a strategy will entail.

-ralph




Binary File I/O with variable string member

by erewhon » Thu, 22 Dec 2005 22:25:04 GMT

On Thu, 22 Dec 2005 07:22:40 -0600, "Ralph"


<snip>



I would advocate moving on a step from .DBF
- tags (or mnemonics) are very useful


Ahem, Ralph - it is Jerry


Add the fields on the fly - it works


Sort of yes and no there
- black box the source (and updating) of the data and one is pretty
much there

Ultimately all data items have to have a name
.. it is a matter of skill and judgement
(AKA learning from ones mistakes)




Binary File I/O with variable string member

by Jeff Johnson [MVP: VB] » Thu, 22 Dec 2005 23:44:11 GMT






Nah. Ralph needs to be kept in his place....




Binary File I/O with variable string member

by Ralph » Fri, 23 Dec 2005 03:19:03 GMT






the

LOL

Actually being often wrong does the trick.

-ralph




Similar Threads

1. Binary File I/O with variable string member

Hi,

I am working on a small application and dont want to store records in any 
database (access,sql. etc). I want to write & read

information from my own binary file, for this purpose I have declared type 
in my application which is as follows.

Public Type CustDetails
 CustID as long
 FName as string
 LName as string
 Addr as string
 City as string
 State as string
 Country as string
 DateofBirth as date
 Weight as double
 .
 .
 .
 ...etc. etc.
End Type


Now to save disk space I dont want to specify the length of string type 
memebers.

I have declared the customer array and can save that customer array into 
file easily but problem comes while loading this file back to the customer 
array as I dont know what exactly would be the number of records saved in 
this file so that i can redim my array accordingly.

My code for saving and retreving type array as follows


Dim arrCustomer() As CustDetails
Dim fso As New FileSystemObject
Private Const mFileName As String = "Customers.dat"

Private Sub SaveFile()
    If GetUBound(arrCustomer) < 0 Then
        Exit Sub
    End If

    Dim arrCounter As Integer
    Dim FileNum As Integer
    Dim DataFilePath As String
    DataFilePath = App.Path & "\" & mFileName
    FileNum = FreeFile
    Open App.Path & "\" & mFileName For Binary As FileNum

    Put FileNum, , arrCustomer
    Close (FileNum)

    MsgBox "Saved..", vbInformation
End Sub


Public Sub LoadFile()
    Dim FileNum As Integer, RecordNumber As Integer
    Dim DataFilePath As String

    DataFilePath = App.Path & "\" & mFileName
    If fso.FileExists(DataFilePath) Then
        FileNum = FreeFile
        Open App.Path & "\" & mFileName For Binary As FileNum

        ReDim arrCustomer(0) ' this is a problem

        Get FileNum, , arrCustomer
        Close (FileNum)
    End If
End Sub


Please help as I beleive there should be some way of retreiving variable 
type member records. I have searched on the net but couldnt find out the 
solution for this problem


Best regards
VJ 


2. Reading a binary file with a string and a byte variable - Visual Basic/VB

3. Accessing class member variables - properties or variables?

Only expose your private members if you wish them to be publicly acessible, 
that sounded horrible... I only create properties to be externally consumed 
by another class, say I have a class that gets loaded from the database and I 
have a property that accesses the data:

    Public Overridable ReadOnly Property SomeData() As String
        Get
            Return m_SomeData
        End Get
    End Property

I'm going to poulate this properties value using the private member 
m_SomeData, that way I don't interfere with anyone else's implementation 
should they descide to override this property.

"dwok" wrote:

>  I have been wondering this for a while now. Suppose I have a class
> that contains some private member variables. How should I access the
> variables throughout the class? Should I use properties that expose the
> variables or is it OK to just access the variables directly? Keep in
> mind that I am talking about accessing the variables from within the
> class that they are defined. Thanks!
> 
> 

4. Replace, in Word saved file, CHANGE variable containing ADODB binary stream (byte array) - Visual Basic/VB

5. Byte order of Long variable in Binary File

On reading Intiger Variables (4 bytes) from BBC micro 
files, into VBA Long variables (4 bytes), I notice that 
the byte order in which VBA reads the variable (using Get)
is in reverse to that used by the BBC micro.

i.e. On the disk

     The BBC stores the 4 bytes in order MSB to LSB.
     Decimal -2 is Hex FFFFFFFE

     The VBA stores the 4 bytes in order LSB to MSB.
     Decimal -2 is Hex FEFFFFFF

Can VBA be made to Put and Get the right way round?


6. Mark off two variables in a binary file - Visual Basic/VB

7. open binary file and search hex string

Hi... I would like to search for a hex string (for example: "E903") inside a 
binary file...
Although I open the file correctly, how do I search hex values?
Thanks in advance!
Nikos



8. Write a string in a binary file, problem with CAPICOM - VB.Net