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

Reading binary files

Hi,
How to effectively write and read structures from binary files in .Net?
Currently I am using functions like ReadInt32 and the likes to read
data from binary file into each elements of a structure, but this is very
inefficent compared to VB3 where I could use Get method to read the whole
structure data? Is there any similar method in .net?

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #1
1 3013
Have you looked at using serialization? Here's quick example:

[Serializable]
struct MyStruct
{
public int anInt;
public byte aByte;
}

static void Main(string[] args)
{
MyStruct s1;
s1.anInt = int.MaxValue;
s1.aByte = byte.MaxValue;

Stream stream = File.Open(@"c:\test.bin", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, s1);
stream.Close();

stream = File.Open(@"c:\test.bin", FileMode.Open);

MyStruct s2 = (MyStruct)formatter.Deserialize(stream);

Console.WriteLine("anInt = {0}\naByte = {1}", s2.anInt, s2.aByte);
}

Check out
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemserializableattributeclasstopic.asp for more info on the
SerializableAttribute class.

hth

-Joel
--------------------
From: "Manjunath sp via DotNetMonster.com" <fo***@nospam.DotNetMonster.com>
Subject: Reading binary files
Date: Fri, 22 Apr 2005 11:59:50 GMT
Organization: http://www.DotNetMonster.com
Message-ID: <e4******************************@DotNetMonster.co m>
X-Abuse-Report: http://www.DotNetMonster.com/Uwe/NB/Abuse.aspx
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: mail.advenet.com 216.32.72.34
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.csharp:93661
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hi,
How to effectively write and read structures from binary files in .Net? Currently I am using functions like ReadInt32 and the likes to read
data from binary file into each elements of a structure, but this is very
inefficent compared to VB3 where I could use Get method to read the whole
structure data? Is there any similar method in .net?

--
Message posted via http://www.dotnetmonster.com


This reply is provided AS IS, without warranty (express or implied).

Nov 17 '05 #2

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

Similar topics

2
by: christos panagiotou | last post by:
hi all I am trying to open some .raw files that represent images (256x256, 8 bit per pixel, no header) in a c++ program I cannot copy paste the module here as it uses a method from the VTK...
3
by: Olivier Maurice | last post by:
Hi all, I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a printer by putting the file printed...
7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
5
by: rnorthedge | last post by:
I am working on a code library which needs to read in the data from large binary files. The files hold int, double and string data. This is the code for reading in the strings: protected...
7
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...
2
by: amfr | last post by:
On windows, is there anything special I have to do to read a binary file correctly?
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
2
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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...

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.