Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading and writing Binary Data

Posted on 1998-05-07
3
Medium Priority
?
140 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

885 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