473,498 Members | 703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary file

How can I write/read a structure into a binary file?

Thanks

Durand
Nov 15 '05 #1
3 1478
There are a few ways to do this, depending on how much control you want.
The easiest is to use the [Serializable] attribute. For example,

[Serializable]
public class Ubiquity
{
private Foo f;
public Ubiquity(Foo F)
{
this.f = f;
}
// some other stuff
}

[Serializable]
public class Foo
{
private int a;
public Foo(int A)
{
this.a = A;
}
// class stuff
}

now, when you want to read/write instances of this class:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class Testing
{
public static void Main(string[] args)
{
Ubiquity u = new Ubiquity(new Foo(100));

// write u to a binary file
// copied from
"ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpguide/html/cpconbasicserialization.htm"

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();

// read the object back from the file
stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read,
FileShare.Read);
MyObject obj = (Ubiquity) formatter.Deserialize(stream);
stream.Close();
}
}

Note that this is an error situation if we don't have [Serializable]
just before class Foo, because Foo will be serialized as well.

See also: ISerializable interfae, SOAP/XML serialization

Durand wrote:
How can I write/read a structure into a binary file?

Thanks

Durand


Nov 15 '05 #2
Hello

In addition to the method in the previous post, you can check the
System.IO.BinaryWriter and System.IO.BinaryReader classes the would provide
better performance and smaller file size than serialization, but require
more work from your side

Best Regards

Sherif

"Durand" <ca***********@terra.com.br> wrote in message
news:#G**************@tk2msftngp13.phx.gbl...
How can I write/read a structure into a binary file?

Thanks

Durand

Nov 15 '05 #3
If there are certain members of a class that don't need to be serialized (or
can't), then mark them with the NonSerializable attribute. This is required
on some things like delegates, or framework classes that don't serialize.

Chris

"Ben Taitelbaum" <be*@jbmsystems.com> wrote in message
news:K5*****************@nwrdny01.gnilink.net...
There are a few ways to do this, depending on how much control you want.
The easiest is to use the [Serializable] attribute. For example,

[Serializable]
public class Ubiquity
{
private Foo f;
public Ubiquity(Foo F)
{
this.f = f;
}
// some other stuff
}

[Serializable]
public class Foo
{
private int a;
public Foo(int A)
{
this.a = A;
}
// class stuff
}

now, when you want to read/write instances of this class:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class Testing
{
public static void Main(string[] args)
{
Ubiquity u = new Ubiquity(new Foo(100));

// write u to a binary file
// copied from
"ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpguide/html/cpconbasicseria
lization.htm"
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();

// read the object back from the file
stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read,
FileShare.Read);
MyObject obj = (Ubiquity) formatter.Deserialize(stream);
stream.Close();
}
}

Note that this is an error situation if we don't have [Serializable]
just before class Foo, because Foo will be serialized as well.

See also: ISerializable interfae, SOAP/XML serialization

Durand wrote:
How can I write/read a structure into a binary file?

Thanks

Durand

Nov 15 '05 #4

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

Similar topics

13
15190
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
28
2739
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
9
6496
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
3
2113
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write...
5
5296
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the...
12
5831
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
7
6027
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
68
5149
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
10
22577
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
16
4458
by: Erwin Moller | last post by:
Why is a binary file executable? Is any binary file executable? Is only binary file executable? Are all executable files binary? What is the connection between the attribute of binary and that of...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.