Board index » delphi » How do you copy a binary file?

How do you copy a binary file?

As a 'newbie' to Delphi (notice the single speach marks!)
I wondered if anyone knows the code for copying binary
files. The book i've got only explains copying text files.

Ken

 

Re:How do you copy a binary file?


Try looking at the FILEMANEX demo program in the Delphi\Demos\doc directory

Chris Arscott
 ----------
From: ken
To: comp.lang.pascal.delphi.misc
Subject: How do you copy a binary file?
Date: 07 June 1996 16:38

As a 'newbie' to Delphi (notice the single speach marks!)
I wondered if anyone knows the code for copying binary
files. The book i've got only explains copying text files.

Ken

// DRS Data & Research  Services plc,    Sunrise Parkway
// Linford Wood,   Milton Keynes,   MK14 6LR.    England
// Phone: +44 1908 666088           Fax: +44 1908 607668
// E-Mail: chr...@drs.co.uk    Web Page: www.drs.co.uk

Re:How do you copy a binary file?


Quote
ken wrote:

> As a 'newbie' to Delphi (notice the single speach marks!)
> I wondered if anyone knows the code for copying binary
> files. The book i've got only explains copying text files.

        You copy a binary file bit by bit.

        Sorry, couldn't help it. Actually you do copy a binary
file chunk by chunk: Make an array of bytes or something for a
buffer, open the source and destination files (with
record size = 1!), and go through the source file, copying the
data to the buffer with BlockRead and writing it to the destination
file with BlockWrite. (Note that BlockRead returns the number of
bytes actually read - you pay attention to this to keep from
writing more data to the destination than the source actually
contained.) A buffer about 30,000 bytes seems to work pretty well.

        Or you could use a TFileStream with LoadFromFile and
SaveToFile.

        That's what the story was in the old days, anyway. In
Win32 there's a CopyFile function you can use (I've never
understood why it wasn't there before).

--
David Ullrich
Sig file accidentally deleted - sorry.

Re:How do you copy a binary file?


Quote
CHRISA (Chris Arscott) wrote:

> Try looking at the FILEMANEX demo program in the Delphi\Demos\doc directory
> As a 'newbie' to Delphi (notice the single speach marks!)
> I wondered if anyone knows the code for copying binary
> files. The book i've got only explains copying text files.

> Ken

If you just want a code fragment on how to quickly accomplish
this, look under the help topic "BlockRead" in the Example.
This will copy a binary (and ANY OTHER TYPE of file...)

Jeff

Re:How do you copy a binary file?


Quote
ken wrote:

> As a 'newbie' to Delphi (notice the single speach marks!)
> I wondered if anyone knows the code for copying binary
> files. The book i've got only explains copying text files.

> Ken

try this:

unit Filecopy;

interface

Uses
  Classes, SysUtils;

procedure CopyFile(const src, dest: String);

implementation

const
  BufSize = 4096;

procedure CopyFile(const src, dest: String);
var
  S, D: TStream;
  B: PChar;
  N: Integer;
begin
  GetMem(B, BufSize);
  Try
    S := TFileStream.Create(Src, fmOpenRead);
    Try
      D := TFileStream.Create(Dest, fmCreate);
      Try
        Repeat
          N := S.Read(B^, BufSize);
          D.Write(B^, N);
        Until N < BufSize;
      Finally
        D.Free;
      End;
    finally
      S.Free;
    end;
  finally
    FreeMem(B, BufSize);
  End
end;

end.

-----------------------------------
Arjan Lamaker
IMAG-DLO Wageningen The Netherlands
mail: e.j.j.lama...@imag.dlo.nl
http://huizen.dds.nl/~lamaker/
------------------------------

-----

Re:How do you copy a binary file?


Take a look at the example in the help for BlockRead for an example of
reading/writing binary files.

John.

Re:How do you copy a binary file?


CHRISA (Chris Arscott) <chr...@drs.co.uk> wrote:

Quote
>As a 'newbie' to Delphi (notice the single speach marks!)
>I wondered if anyone knows the code for copying binary
>files. The book i've got only explains copying text files.

Use the Windows API calls

Add LZExpand to your uses clause.

Use the LZOpenFile() function to open the source and destination files
Use the LZCopy() function to copy from source to destination
Use the LZClose() function to close the files

For a complete description and code sample, see Delphi Developer's
Guide by Pacheco & Teixeira (page 770)
==
Bob Small

Other Threads