Solved

Reading and writing Binary Data

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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