473,385 Members | 1,333 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,385 software developers and data experts.

How to write a struct to a file?

Hi,
What is the easiest way to write a struct(with biary data) to a file ?
do I need to covert it through e.g. Marshal class ?
Thanks for any tips!

DaeSuk.
Nov 16 '05 #1
3 14474
I think this will do it:
byte [] buff = null;
unsafe
{
buff = new byte [sizeof(MyStruct)];
}
IntPtr ptr = Marshal.AllocHGlobal(buff.Length);
Marshal.StructureToPtr(myStruct, ptr, true);
Marshal.Copy(ptr, buff, 0x0, buff.Length);
Marshal.FreeHGlobal(ptr);
FileStream fs = new FileStream("C:\\MyStruct.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
fs.Write(buff, 0x0, buff.Length);
fs.Close();


"Dae-Suk Chung" <ch**********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,
What is the easiest way to write a struct(with biary data) to a file ?
do I need to covert it through e.g. Marshal class ?
Thanks for any tips!

DaeSuk.

Nov 16 '05 #2
Dennis,

While this might do it, if the structure contains pointers, it is not
going to accurately write the structure, as it won't know how to dereference
the pointer information. Also, this assumes that the structure can be
marshaled correctly across the managed/unmanaged boundary (which is also
more expensive).

I would recommend appending the Serializable attribute to the structure,
and then passing the structure to a BinaryFormatter (for a compact, fast,
format) or a SoapFormatter (for an XML document), and use that.

If your structure isn't complex, you could even use the XmlSerializer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:DO******************@news4.e.nsc.no...
I think this will do it:
byte [] buff = null;
unsafe
{
buff = new byte [sizeof(MyStruct)];
}
IntPtr ptr = Marshal.AllocHGlobal(buff.Length);
Marshal.StructureToPtr(myStruct, ptr, true);
Marshal.Copy(ptr, buff, 0x0, buff.Length);
Marshal.FreeHGlobal(ptr);
FileStream fs = new FileStream("C:\\MyStruct.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
fs.Write(buff, 0x0, buff.Length);
fs.Close();


"Dae-Suk Chung" <ch**********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,
What is the easiest way to write a struct(with biary data) to a file ?
do I need to covert it through e.g. Marshal class ?
Thanks for any tips!

DaeSuk.


Nov 16 '05 #3
I see, thanks,
one more question then,
what I want to do is to be able to write the structs to a file, ie. append
but also to position myself using seek and update a record.
can I achieve this using a serializable struct and BinaryFormatter ?
if not, the first option using the unsafe codes seems to be the only option
?
Thanks again for any advice!
DaeSuk.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Dennis,

While this might do it, if the structure contains pointers, it is not
going to accurately write the structure, as it won't know how to dereference the pointer information. Also, this assumes that the structure can be
marshaled correctly across the managed/unmanaged boundary (which is also
more expensive).

I would recommend appending the Serializable attribute to the structure, and then passing the structure to a BinaryFormatter (for a compact, fast,
format) or a SoapFormatter (for an XML document), and use that.

If your structure isn't complex, you could even use the XmlSerializer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dennis Myr?" <de****@oslokb.no> wrote in message
news:DO******************@news4.e.nsc.no...
I think this will do it:
byte [] buff = null;
unsafe
{
buff = new byte [sizeof(MyStruct)];
}
IntPtr ptr = Marshal.AllocHGlobal(buff.Length);
Marshal.StructureToPtr(myStruct, ptr, true);
Marshal.Copy(ptr, buff, 0x0, buff.Length);
Marshal.FreeHGlobal(ptr);
FileStream fs = new FileStream("C:\\MyStruct.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
fs.Write(buff, 0x0, buff.Length);
fs.Close();


"Dae-Suk Chung" <ch**********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,
What is the easiest way to write a struct(with biary data) to a file ?
do I need to covert it through e.g. Marshal class ?
Thanks for any tips!

DaeSuk.



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas | last post by:
What's the quickest way to write and read 10.000 integer values ( or more ) to and from a file? Using struct somehow? The example in the docs shows how to handle to or three arguments, but is the...
3
by: Cesar Andres Roldan Garcia | last post by:
Hi I'm trying to write an hexadecimal file... I mean not a text plain... I have to convert a float decimal number in float hexadecimal one, and that's done. That number is the one I'm gonna...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
5
by: Dennis Schulz | last post by:
hi all, im a very beginner in c language and this is my try in writing / appending a struct in a file. unfortuately nothing is written into the file. especially the part with the pointer...
27
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
2
by: Tiger | last post by:
I try to write a struct into a brut file but I can't write all var in this struct when I try to add user I have that : > testmachine:/usr/share/my_passwd# ./my_passwd -a users.db > Ajout d'une...
7
by: nass | last post by:
hi all, i am running slackware linux and need to use some function that will will enable me to write and read from a shared mem segment.. i am using open() , to open a file, and then use mmap to...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
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: 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.