473,725 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = BytesToYourStru ct(array);
tw.WriteLine(re c.abs_pot_no + " " + rec.md.Blev);

}

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

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

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

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

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

PRD_record s;
IntPtr arrayPointer=Ma rshal.UnsafeAdd rOfPinnedArrayE lement
(arr,0);
s=(PRD_record)M arshal.PtrToStr ucture(arrayPoi nter,typeof
(PRD_record));
return s;

}
}
Nov 15 '05 #1
1 2570
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******@hotma il.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 = BytesToYourStru ct(array);
tw.WriteLine(re c.abs_pot_no + " " + rec.md.Blev);

}

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

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

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

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

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

PRD_record s;
IntPtr arrayPointer=Ma rshal.UnsafeAdd rOfPinnedArrayE lement
(arr,0);
s=(PRD_record)M arshal.PtrToStr ucture(arrayPoi nter,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
8902
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 (Visualization Toolkit) the module i ve got is already tested with the same dataset and it works if these files have been created under windows and copied in linux (i am
3
2820
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 to the printer to standard in. You have to provide yourself the program that does something with that data, so I thought with some quick c/c++ programming that would be no problem. I tried dozens of ways of reading the data from stdin or cin,...
7
4219
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, which in matlab is 32 bits. How do I get this binary file in to floats in c++? I try reading the file using the ifstream>>myFloat. But nothing ever goes in to the float. So the closest I have come is having 4 unsigned char store the binary data....
50
5004
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
5
6451
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 internal override string ReadString() { stringLength = fileStream.ReadByte(); moInput.Read(byteArrayBuffer, 0, stringLength); return asciiEncoding.GetString(byteArrayBuffer, 0, stringLength ); }
7
6062
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
2
1899
by: amfr | last post by:
On windows, is there anything special I have to do to read a binary file correctly?
30
4575
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
3250
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 between plain ASCII files I receive and the Unicode ones would be to check if the first two bytes read 0xFFFE. But nothing I do seems to be able to do that. I tried reading it in binary mode and reading two characters in:
6
5270
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
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.