ReadWriteFile (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the reading and writing of text files provided by methods of TFile class.

Code

void __fastcall TForm1::btnOpenClick(TObject *Sender)
{
	/* Read text file from given path and write in memo */
	try
	{
		mmText->Lines->Strings[0] = TFile::ReadAllText(edPath->Text);
	}
	catch(...)
	{
		/* Catch the possible exceptions */
		MessageDlg("Incorrect path", mtError, TMsgDlgButtons() << mbOK, 0);
		return;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSaveClick(TObject *Sender)
{
	/* Write text from memo to file from given path */
	try
	{
		TFile::WriteAllText(edPath->Text, mmText->Lines->Text);
	}
	catch(...)
	{
		/* Catch the possible exceptions */
		MessageDlg("Incorrect path", mtError, TMsgDlgButtons() << mbOK, 0);
		return;
	}
}
//---------------------------------------------------------------------------

Uses