Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Leaders
Posted

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. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...