Solved

Reading and writing Binary Data

Posted on 1998-05-07
3
128 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys.  I think that pretty much covers what I needed to confirm.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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