Show: Delphi C++
Display Preferences

Copying Data From One Stream To Another

From RAD Studio
Jump to: navigation, search

Go Up to Developing Applications with VCL Components

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 (C++)

  1. Choose File > New > Other > 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 both the Caption and Name properties.

To create a VCL Form with a button control (Delphi)

  1. Choose File > New > Other > Delphi 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 both the Caption and Name properties.

To write the copy stream procedure (Delphi)

  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. Place the cursor before the begin reserved word; then press return. This creates a new line above the code block.
  4. Insert the cursor on the new line created and type the following variable declaration:
var stream1, stream2: TStream;
  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;

To write the copy stream procedure (C++)

  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. Enter the following variable declarations:
TStream *stream1, *stream2;
  1. Insert the cursor within the code block, and type the following code:
  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.
  2. Choose Run > Run to build and run the application. The form displays with a button called CopyFile.
  3. Click CopyFile.
  4. 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.

See Also

Personal tools
In other languages