473,382 Members | 1,271 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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
5 4482
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fredrik Normann | last post by:
Hello, I'm trying to read the binary files under /var/spool/rwho/ so I'm wondering if anyone has done that before or could give me some clues on how to read those files. I've tried to use the...
3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been able to read data from binary files to filestrean, the data in the files are structured, what I'm trying to do is to loop through the binary files and add data to my...
1
by: Andrea Gavana | last post by:
Hello NG, that may sound a silly question, but I didn't find anything really clear about the issue of reading unformatted big endian files with Python. What I was doing till now, was using...
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
4
by: Mastastealth | last post by:
I'm trying to create a program to read a certain binary format. I have the format's spec which goes something like: First 6 bytes: String Next 4 bytes: 3 digit number and a blank byte --- Next...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.