Binary Writing from multiple objects

kejpa

Well-known member
Joined
Oct 10, 2003
Messages
320
Hi,
I started out with a simple q about howto write a struct to a file but ended up remaking it all. Now Im all .NET IO :-)

Still some questions arise.
I have one file that I want to write the following Struct
Code:
	Public Nodeid As Byte	  1-127dec
	Public SyncTime As Single    sec since midnight
	Public Value1 As Short		 +/-30000
	Public Value2 As Short		 +/-30000
	Public Value3 As Short	  +/-30000
from at least two separate objects.

1. How do I convert this struct to something that a BinaryWriter can write?
2. Whos the "best owner" of the file? Each object, the collection holding the objects or the application form/main ?!?
Currently each object own its logfile, thus I can use a separate if I want and many objects can share one if I prefer that. But Im having second thoughts.
3. The struct should be frequently added to the end of the file. Previously I managed that by having a Long at the first position telling where to write the next struct. Is this still the best approach?

:confused: but smiling
/Kejpa
 
I usually make a little method in the struct that will write it to a binarywriter and read it from the binaryreader.
Public Sub ToFile(BR As BinaryReader)
Nodeid = BR.ReadByte()
SyncTime = BR.ReadSingle()
Value1 = BR.ReadInt16()
etc.
End Sub
:)

By owning the file, what do you mean? You can have the form to hold the binary readers declarations, but I dont think the object should own the file (though, not quite sure what you mean by ownership either).
I believe you can use the BinaryReaders .BaseStream.Seek() to go to the end of the file. :)
 
Iceplug said:
By owning the file, what do you mean?
Alright, maybe Im too involved in real estate at the moment ;)
By owner I mean the object in which the file is managed.
The class in which the values are received could raise events and have the objects collection class or the applications form/main to write the logging information.

When crawling the net I found that BitConverter can help me convert my struct to byte data, I guess that will solve at least one small part of my tasks

Regards
/Kejpa
 
I would say that serialization is the best way to write objects to a file. If you want to know more about it, just reply :).
 
Back
Top