Reading a String and Writing It To a File
Go Up to How To Build Windows VCL Applications
Creating this VCL application consists of the following steps:
- Create a VCL Form with a button control.
- Write the code to read the string and write it to a file.
- Run the application.
To create a VCL Form
- Create a directory in which to store your project files.
- Choose File > New > Windows VCL Application - Delphi .
- From the Standard page of the Tool palette, place a TButton component on the form.
To read and write a string
- Select Button1 on the form.
- 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.
- For Delphi. place the cursor before the begin reserved word; then press return. This creates a new line above the code block.
- Type the following variable declarations: TFileStream *fs const AnsiString str = "Hello";
- 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
- Save your project files; then choose Run > Run to build and run the application. The form displays with a button called Button1.
- Click Button1.
- 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.