StreamStrRdWr (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of the TStreamReader and TStreamWriter classes. WriteLine and Write are used to store the texts into the file and ReadLine and ReadToEnd are used to load the stored contents.

Code

void __fastcall TMainForm::btSaveClick(TObject *Sender)
{
	TStreamWriter* writer;

	/* Create a new stream writer directly. */
	writer = new TStreamWriter("local_file.txt",
		false, TEncoding::UTF8, 1024);

	/* Store the title and then the text. */
	writer->WriteLine(edtTitle->Text);
	writer->Write(mmText->Text);

	/* Close and Free the writer. */
	delete writer;
}

void __fastcall TMainForm::btLoadClick(TObject *Sender)
{
	TStreamReader* reader;

	/* Create a new stream writer directly. */
	reader = new TStreamReader("local_file.txt",
		TEncoding::UTF8);

	/* Read the title and then the actual text. */
	edtTitle->Text = reader->ReadLine();
	mmText->Text = reader->ReadToEnd();

	/* Close and Free the writer. */
	delete reader;
}

Uses