Solved

Reading and writing Binary Data

Posted on 1998-05-07
3
135 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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