467,885 Members | 1,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,885 developers. It's quick & easy.

Reading binary struct

Hello
how can i creat e structure and read the struct from a binary file?
binaryreader doesn't accept objects so i have to read like reader.getbyte(),
reader.getint32(),... . but it should be a way to create a class líke
FileHeader and read compelet header at once, like C++ .
do you know how?
Nov 16 '05 #1
  • viewed: 4190
Share:
5 Replies
Hello,

Use binary serialization.
Mark your structure as [Serializable] and use the
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter class.

--
Andriy Gnennyy
pulsar2003@{no-spam}email.ru (Please remove {no-spam} for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx

"Blmn" <bluemanREMOVETHIS[@]gmail.com> wrote in message
news:e5**************@TK2MSFTNGP15.phx.gbl...
Hello
how can i creat e structure and read the struct from a binary file?
binaryreader doesn't accept objects so i have to read like reader.getbyte(), reader.getint32(),... . but it should be a way to create a class líke
FileHeader and read compelet header at once, like C++ .
do you know how?

Nov 16 '05 #2
not possible.
seialization only works when the file being created by serialization. then
they can be deserialized. it can't apply to ordinary files.
actually i don't create these files, i just read'em. the program that create
these files is uder unix and works from 1989. can't even dream about
changing that program.
"Andriy Gnennyy" <pulsar2003@{no-spam}email.ru> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
Hello,

Use binary serialization.
Mark your structure as [Serializable] and use the
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter class.

--
Andriy Gnennyy
pulsar2003@{no-spam}email.ru (Please remove {no-spam} for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx

"Blmn" <bluemanREMOVETHIS[@]gmail.com> wrote in message
news:e5**************@TK2MSFTNGP15.phx.gbl...
Hello
how can i creat e structure and read the struct from a binary file?
binaryreader doesn't accept objects so i have to read like

reader.getbyte(),
reader.getint32(),... . but it should be a way to create a class líke
FileHeader and read compelet header at once, like C++ .
do you know how?


Nov 16 '05 #3
Blmn

How about identifying the byte size of your structure, reading a chink of
bytes using ReadBytes ( I hope this method exists) and then mapping to the
fields in your structure?

Cheers

Raj

"Blmn" <bluemanREMOVETHIS[@]gmail.com> wrote in message
news:e5**************@TK2MSFTNGP15.phx.gbl...
Hello
how can i creat e structure and read the struct from a binary file?
binaryreader doesn't accept objects so i have to read like reader.getbyte(), reader.getint32(),... . but it should be a way to create a class líke
FileHeader and read compelet header at once, like C++ .
do you know how?

Nov 16 '05 #4
The Marshal.PtrToStructure class could be useful in this case. But it
seems you will have to enter the relam of unsafe code. I found some more
articles that might be useful

http://codenewbie.com/tutorials/1852.html
http://www.error-bank.com/microsoft....bl_Thread.aspx
http://www.dotnetjunkies.com/How%20T...3A2E3D085.dcik
http://dotnet247.com/247reference/msgs/39/199789.aspx
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

Blmn wrote:
Hello
how can i creat e structure and read the struct from a binary file?
binaryreader doesn't accept objects so i have to read like reader.getbyte(),
reader.getint32(),... . but it should be a way to create a class líke
FileHeader and read compelet header at once, like C++ .
do you know how?

Nov 16 '05 #5
Actually, when you mark an object as serializable and then write it using
the binary formatter, it doesn't put "special" markings stating the object
type. It just writes the data. If you want to, you can read the data from
the data file into a byte array, rearrange the bytes to match the
expectations of the deserializer, create a memory stream from your modified
byte array, and deserialize to the object from the stream.

Honestly, I wouldn't think it to be worth doing, but that's your call.

--- Nick

"Blmn" <bluemanREMOVETHIS[@]gmail.com> wrote in message
news:O7**************@TK2MSFTNGP12.phx.gbl...
not possible.
seialization only works when the file being created by serialization. then
they can be deserialized. it can't apply to ordinary files.
actually i don't create these files, i just read'em. the program that create these files is uder unix and works from 1989. can't even dream about
changing that program.
"Andriy Gnennyy" <pulsar2003@{no-spam}email.ru> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
Hello,

Use binary serialization.
Mark your structure as [Serializable] and use the
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter class.

--
Andriy Gnennyy
pulsar2003@{no-spam}email.ru (Please remove {no-spam} for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx

"Blmn" <bluemanREMOVETHIS[@]gmail.com> wrote in message
news:e5**************@TK2MSFTNGP15.phx.gbl...
Hello
how can i creat e structure and read the struct from a binary file?
binaryreader doesn't accept objects so i have to read like

reader.getbyte(),
reader.getint32(),... . but it should be a way to create a class líke
FileHeader and read compelet header at once, like C++ .
do you know how?



Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Fredrik Normann | last post: by
3 posts views Thread by muser | last post: by
6 posts views Thread by KevinD | last post: by
1 post views Thread by Jón Sveinsson | last post: by
1 post views Thread by Andrea Gavana | last post: by
16 posts views Thread by Jm.GlezdeRueda | last post: by
13 posts views Thread by swetha | last post: by
6 posts views Thread by efrenba | last post: by
4 posts views Thread by Mastastealth | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.