RAD Studio
ContentsIndex
PreviousUpNext
Copying Data From One Stream To Another

Creating this VCL application consists of the following steps:

  1. Create a project directory containing a text file to copy.
  2. Create a VCL Form with a button control.
  3. Write the code to read the string and write it to a file.
  4. Run the application.

To set up your project directory and a text file to copy

  1. Create a directory in which to store your project files.
  2. Using a text editor, create a simple text file and save it as from.txt in your project directory.

To create a VCL Form with a button control

  1. Choose FileNewOtherDelphi Projects or C++Builder Projects and double-click the VCL Forms Application icon. The VCL Forms Designer is displayed.
  2. From the Standard page of the Tool palette, place a TButton component on the form.
  3. In the Object Inspector, enter CopyFile for the Caption and Name properties.

To write the copy stream procedure

  1. Select Button1 on the form.
  2. In the Object Inspector, double-click the OnClick action on the Events tab. The Code Editor displays, with the cursor in the TForm1.CopyFileClick (Delphi) or TForm1::CopyFileClick (C++) event handler block.
  3. For Delphi, Place the cursor before the begin reserved word; then press return. This creates a new line above the code block.
  4. For Delphi, insert the cursor on the new line created and type the following variable declaration:

var stream1, stream2: TStream;

For C++, enter the following variable declarations:

TStream *stream1, *stream2;

  1. Insert the cursor within the code block, and type the following code:

stream1 := TFileStream.Create('from.txt', fmOpenRead);
try
  stream2:= TFileStream.Create('to.txt', fmCreate);
  try
    stream2.CopyFrom(stream1, stream1.Size);
  finally
    stream2.Free;
  end;
finally
  stream1.Free;
end;
stream1 = new TFileStream( “from.txt”, fmOpenRead );
try {
 stream2 = new TFileStream( “to.txt”, fmCreate );
     try {
   stream2–>CopyFrom( stream1, stream1–>Size );
 } __finally {
   stream2–>Free();
 }
} finally {
 stream1–>Free();
}
To run the application

  1. Save your project files; then choose RunRun to build and run the application. The form displays with a button called CopyFile.
  2. Click CopyFile.
  3. Use a text editor to open the newly created file to.txt, which is located in your project directory. The contents of from.txt are copied into to.txt.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!