Solved

Reading and writing Binary Data

Posted on 1998-05-07
3
129 Views
Last Modified: 2010-04-06
I have a file with a 128 byte header field, followed by RFC-822 text.  How would I go about reading in the binary header data from this file so that the data can be manipulated and/or added to the top of another existing text file.
0
Comment
Question by:sparklin
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
inter earned 100 total points
ID: 1342562
Hi,
I propese the following code fragments:

READING BINARY DATA(128byte)
-------------------------------------
This code returns the first 128 bytes of a file named F in variable H:

type
 THeader = array[0..127] of byte;

function ReadHeader(F : string; var H : THeader):boolean;
var
  F : TFileStream;
begin
  F := TFileStream.Create(F, fmOpenRead);
  try
    F.Read(H, SizeOf(THeader));
    Result := true;
  finally
    F.Free;
  end;
end;

GIVEN HEADER READED BY ABOVE APPEND IT TO ANOTHER TEXT FILE

This routine adds the given header to the top of existing text file named F
and create a new file named N.

function AttachHeader(F,N : string; const H : THeader):boolean;
var
  T : TStringList;
  M : TMemoryStream;
begin
  F := TFileStream.Create(N, fmCreate or fmOpenWrite);
  M := TMemoryStream.Create;
  try
    //read text file
    M.LoadFromFile(F);
    //Write header to new file
    F.Write(H, SizeOf(THeader));
    //Append text file to the end of new file
    M.SaveToStream(F);
    Result := true;
  finally
    M.Free;
    F.Free;
  end;
end;


Regards,
Igor
0
 
LVL 4

Expert Comment

by:d003303
ID: 1342563
Hi d003303, (and sparklin ofcourse)
Did I understand the question wrong. If so, excuse me to post it as answer.
Regards,
Igor
0
 
LVL 5

Expert Comment

by:inter
ID: 1342564
Thanks guys.  I think that pretty much covers what I needed to confirm.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now