473,397 Members | 2,099 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,397 software developers and data experts.

Reading through binary files

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 struct, in this file there are lot of
records i need to loop trough, first time I read the
file, the data is fine but for the rest the data is not
correct
here is my code for reading the binary files and loop
through the code, what is wrong with the code

public static void Main()
{
PRD_record rec = new PRD_record();//struct which
has been written to file

FileStream fs = new FileStream
(@"c:\elas.dat",FileMode.Open);//Filestream to read text
TextWriter tw = new StreamWriter
(@"c:\elas.txt");//Textwriter to write out text

byte[] array = new byte[Marshal.SizeOf(rec)];

while((fs.Read(array,0,Marshal.SizeOf(rec))!=0))
{
rec = BytesToYourStruct(array);
tw.WriteLine(rec.abs_pot_no + " " + rec.md.Blev);

}

fs.Close();
tw.Close();

}
//Converting my structure to bytes
static byte[] YourStructToBytes( PRD_record s )
{

int size = Marshal.SizeOf( s );
byte[] retArr = new byte[ size ];
IntPtr buf = Marshal.AllocHGlobal( size ); // create
unmanaged memory
Marshal.StructureToPtr ( s, buf, false ); // copy
struct

for (int i=0; i<size; ++i)
{
retArr[i] = Marshal.ReadByte(buf, i); // read
unmanaged bytes
}
Marshal.FreeHGlobal( buf );
return retArr;
}

//Converting my bytes to struct
static PRD_record BytesToYourStruct( byte[] arr )
{

PRD_record s;
IntPtr arrayPointer=Marshal.UnsafeAddrOfPinnedArrayElemen t
(arr,0);
s=(PRD_record)Marshal.PtrToStructure(arrayPointer, typeof
(PRD_record));
return s;

}
}
Nov 15 '05 #1
1 2516
I think you should take a look at object serialization. A usefull article
can be http://www.c-sharpcorner.com/vbnet/o...ialization.asp

good luck!
Steven.
"Jón Sveinsson" <ru******@hotmail.com> wrote in message
news:05****************************@phx.gbl...
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 struct, in this file there are lot of
records i need to loop trough, first time I read the
file, the data is fine but for the rest the data is not
correct
here is my code for reading the binary files and loop
through the code, what is wrong with the code

public static void Main()
{
PRD_record rec = new PRD_record();//struct which
has been written to file

FileStream fs = new FileStream
(@"c:\elas.dat",FileMode.Open);//Filestream to read text
TextWriter tw = new StreamWriter
(@"c:\elas.txt");//Textwriter to write out text

byte[] array = new byte[Marshal.SizeOf(rec)];

while((fs.Read(array,0,Marshal.SizeOf(rec))!=0))
{
rec = BytesToYourStruct(array);
tw.WriteLine(rec.abs_pot_no + " " + rec.md.Blev);

}

fs.Close();
tw.Close();

}
//Converting my structure to bytes
static byte[] YourStructToBytes( PRD_record s )
{

int size = Marshal.SizeOf( s );
byte[] retArr = new byte[ size ];
IntPtr buf = Marshal.AllocHGlobal( size ); // create
unmanaged memory
Marshal.StructureToPtr ( s, buf, false ); // copy
struct

for (int i=0; i<size; ++i)
{
retArr[i] = Marshal.ReadByte(buf, i); // read
unmanaged bytes
}
Marshal.FreeHGlobal( buf );
return retArr;
}

//Converting my bytes to struct
static PRD_record BytesToYourStruct( byte[] arr )
{

PRD_record s;
IntPtr arrayPointer=Marshal.UnsafeAddrOfPinnedArrayElemen t
(arr,0);
s=(PRD_record)Marshal.PtrToStructure(arrayPointer, typeof
(PRD_record));
return s;

}
}

Nov 15 '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: 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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...
0
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...

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.