471,066 Members | 1,020 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 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 2257
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by christos panagiotou | last post: by
3 posts views Thread by Olivier Maurice | last post: by
7 posts views Thread by laclac01 | last post: by
50 posts views Thread by Michael Mair | last post: by
7 posts views Thread by John Dann | last post: by
2 posts views Thread by amfr | last post: by
30 posts views Thread by siliconwafer | last post: by
2 posts views Thread by nnimod | last post: by
6 posts views Thread by arne.muller | last post: by

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.