kejpa Posted June 14, 2004 Posted June 14, 2004 Hi, I started out with a simple q about howto write a struct to a file but ended up remaking it all. Now I'm all .NET IO :-) Still some questions arise. I have one file that I want to write the following Struct 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. Who's 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 I'm 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 Quote
Leaders Iceplug Posted June 14, 2004 Leaders Posted June 14, 2004 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 reader's declarations, but I don't think the object should own the file (though, not quite sure what you mean by ownership either). I believe you can use the BinaryReader's .BaseStream.Seek() to go to the end of the file. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
kejpa Posted June 15, 2004 Author Posted June 15, 2004 By owning the file' date=' what do you mean? [/quote'] Alright, maybe I'm 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 Quote
*Experts* mutant Posted June 16, 2004 *Experts* Posted June 16, 2004 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 :). Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.