Home

File Processing: VCL File Buffering

 

Introduction

The Visual Component Library supports another technique of file processing. Instead of saving the components as "streamable" objects, it gives you the option of saving the contents of controls. These contents are taken as data and not as VCL components. We will refer to this technique as file buffering.

To process the contents of controls as "streamable" values, the value of each object of the application is taken in its C/C++ context, as a variable created from known data types. The application itself is created like any other:

  

Values Buffering

To create a file, you can use the TFileStream class reviewed earlier, using the same rules. To write data to a file, you can call the TFileStream::WriteBuffer() method. Its syntax is:

void __fastcall WriteBuffer(const void *Buffer, int Count);

The WriteBuffer() method is used when you must let the compiler know the amount of memory space you want to use for a particular variable. It requires two arguments. The first, Buffer, is the value that needs to be saved. The Count parameter specifies the number of bytes that the value will need to the stored.

Here is an example:

//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnSaveClick(TObject *Sender)
{
	TFileStream *Streamer;
	char FullName[40];
	TDateTime DOB;
	Integer Gender;

	strcpy(FullName, edtFullName->Text.c_str());
	DOB = StrToDate(edtDOB->Text);
	Gender = cboGender->ItemIndex;

	if( SaveDialog1->Execute() )
	{
		try {
			Streamer = new TFileStream(SaveDialog1->FileName, fmCreate);
			Streamer->WriteBuffer(&FullName, 40);
			Streamer->WriteBuffer(&DOB, 40);
			Streamer->WriteBuffer(&Gender, 20);
		}
		__finally
		{
			delete Streamer;
		}
	}
}
//---------------------------------------------------------------------------

Value Reading

Data reading in this context is performed using the ReadBuffer() method of the TFileStream class. Its syntax is:

void __fastcall ReadBuffer(void *Buffer, int Count);

The ReadBuffer() method also requires two pieces of information. The Buffer parameter is the value that needs to be read. The Count parameter is used to specify the number of bytes that need to be read for the Buffer value.

Here is an example:

//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnOpenClick(TObject *Sender)
{
	TFileStream *Streamer;
	char FullName[40];
	TDateTime DOB;
	Integer Gender;

	if( OpenDialog1->Execute() )
	{
		try {
			Streamer = new TFileStream(OpenDialog1->FileName, fmOpenRead);
			edtFullName->Text.Delete(0, 40);
			edtDOB->Text.Delete(0, 40);
			cboGender->ItemIndex = 2;

			Streamer->ReadBuffer(&FullName, 40);
			Streamer->ReadBuffer(&DOB, 40);
			Streamer->ReadBuffer(&Gender, 20);

			edtFullName->Text = FullName;
			edtDOB->Text = DateToStr(DOB);
			cboGender->ItemIndex = StrToInt(Gender);
		}
		__finally
		{
			delete Streamer;
		}
	}
}
//---------------------------------------------------------------------------

 

Home Copyright 2005-2007 FunctionX, Inc.