Practical
Learning: Writing Some Values
|
|
- In the Menu Designer, double-click Save
- Implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Save1Click(TObject *Sender)
{
TFileStream *fsIceCreamer;
wchar_t orderDate[40];
wchar_t orderTime[40];
wchar_t flavor[40];
wchar_t container[40];
wchar_t ingredient[40];
wchar_t scoops[20];
wchar_t orderTotal[20];
if( dlgSave->Execute() )
{
try {
StrCopy(orderDate, edtOrderDate->Text.c_str());
StrCopy(orderTime, edtOrderTime->Text.c_str());
StrCopy(flavor, ledFlavor->Text.c_str());
StrCopy(container, ledContainer->Text.c_str());
StrCopy(ingredient, ledIngredient->Text.c_str());
StrCopy(scoops, ledScoops->Text.c_str());
StrCopy(orderTotal, ledOrderTotal->Text.c_str());
fsIceCreamer = new TFileStream(dlgSave->FileName, fmCreate);
fsIceCreamer->WriteBuffer(&orderDate, 40);
fsIceCreamer->WriteBuffer(&orderTime, 40);
fsIceCreamer->WriteBuffer(&flavor, 40);
fsIceCreamer->WriteBuffer(&container, 40);
fsIceCreamer->WriteBuffer(&ingredient, 40);
fsIceCreamer->WriteBuffer(&scoops, 20);
fsIceCreamer->WriteBuffer(&orderTotal, 20);
New1Click(Sender);
}
__finally
{
delete fsIceCreamer;
}
}
}
//---------------------------------------------------------------------------
- To execute, press F9
- Enter some values as follows:
- Clisk Save
- Set the file name to 1001
- Click Save
- Enter some values as follows:
- Click Save
- Set the file name to 1002 and click Save
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.
Practical
Learning: Reading Some Values
|
|
- In the Menu Designer, double-click Open...
- Implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Open1Click(TObject *Sender)
{
TFileStream *fsIceCream;
wchar_t orderDate[40];
wchar_t orderTime[40];
wchar_t flavor[40];
wchar_t container[40];
wchar_t ingredient[40];
wchar_t scoops[20];
wchar_t orderTotal[20];
if( dlgOpen->Execute() )
{
try {
fsIceCream = new TFileStream(dlgOpen->FileName, fmOpenRead);
fsIceCream->ReadBuffer(&orderDate, 40);
fsIceCream->ReadBuffer(&orderTime, 40);
fsIceCream->ReadBuffer(&flavor, 40);
fsIceCream->ReadBuffer(&container, 40);
fsIceCream->ReadBuffer(&ingredient, 40);
fsIceCream->ReadBuffer(&scoops, 20);
fsIceCream->ReadBuffer(&orderTotal, 20);
edtOrderDate->Text = orderDate;
edtOrderTime->Text = orderTime;
ledFlavor->Text = flavor;
ledContainer->Text = container;
ledIngredient->Text = ingredient;
ledScoops->Text = scoops;
ledOrderTotal->Text = orderTotal;
}
__finally
{
delete fsIceCream;
}
}
}
//---------------------------------------------------------------------------
- On the Menu Designer, double-click Exit
- Close the Menu Designer
- Implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Exit1Click(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
- To execute, on the main menu, click Run -> Run
- Using the main menu, open one of the previously saved file
|
|