Reading a String and Writing It To a File

From RAD Studio
Jump to: navigation, search

Go Up to How To Build VCL Forms Applications


Creating this VCL application consists of the following steps:

  1. Create a VCL Form with a button control.
  2. Write the code to read the string and write it to a file.
  3. Run the application.

To create a VCL Form

  1. Create a directory in which to store your project files.
  2. Choose File > New > Other > Delphi Projects or C++Builder Projects and double-click the VCL Forms Application icon. The VCL Forms Designer is displayed.
  3. From the Standard page of the Tool palette, place a TButton component on the form.

To read and write a string

  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.Button1Click (Delphi) or TForm1::Button1Click (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. Type the following variable declarations: TFileStream *fs const AnsiString str = "Hello";
  5. Insert the cursor within the code block, and type the following code: fs = new TFileStream("temp.txt", fmCreate);
 fs->Write ((void*)str.c_str(), str.fmCreate);

To run the "Hello world" application

  1. Save your project files; then choose Run > Run to build and run the application. The form displays with a button called Button1.
  2. Click Button1.
  3. Use a text editor to open the newly created file temp.txt, which is located in your project directory. The string 'Hello' displays in the file.

See Also