Win32 and Object Pascal File Processing |
|
Introduction |
The Object Pascal language available through Embarcadero Delphi provides its own means of performing file processing. It does this through many functions. Fortunately, through the VCL, these functions are available to you and you can use them in a C++Builder application. |
In Microsoft Windows, in most operations, before using a file, you must create it. This is not the same as physically creating a file. It means you must indicate that you want to create a file. To do this in Object Pascal, you can call the FileCreate() function. Because you are not really creating a file, the way you call this function depends on what you intend to do with it. As a result, this function takes one or more arguments. The minimum argument the FileCreate() function takes is the name of the file, which is a string. You can provide just the name of the file or its whole path. In this case, the syntax of the function appears as: int FileCreate(const String FileName); |
When the FileCreate() function has been called, it returns an unsigned integer named a handle. Here is an example: //--------------------------------------------------------------------------- void __fastcall TfrmEvaluation::btnSaveClick(TObject *Sender) { int iFileHandle; // Get a handle to a file iFileHandle = FileCreate(edtFileName->Text); // Write the value of the radius to the file FileWrite(iFileHandle, &dRadius, sizeof(double)); } //--------------------------------------------------------------------------- This handle would be used in subsequent operations.
Once you have a file, you may want to add one or more values to it. To do this in Object Pascal, you can call the FileWrite() function. Its syntax appears as: int FileWrite(int Handle, const void * Buffer, int Count); The first argument is a handle you would have gotten from calling the FileCreate() function. The Buffer argument is the value you want to write to the file. As you see that this is a pointer to void, the value can be anything, but formatting it is the issue. The last argument is the size (the number of bytes) of the value you want to write. Here is an example: //--------------------------------------------------------------------------- void __fastcall TfrmEvaluation::btnSaveClick(TObject *Sender) { int iFileHandle; double dRadius; // Get the value of the radius dRadius = StrToFloat(edtRadius->Text); // Create handle to a file iFileHandle = FileCreate(edtFileName->Text); // Write the value of the radius to the file FileWrite(iFileHandle, &dRadius, sizeof(double)); // Reset the form edtRadius->Text = L""; edtDiameter->Text = L""; edtCircumference->Text = L""; edtArea->Text = L""; edtVolume->Text = L""; } //---------------------------------------------------------------------------
If want to get the values stored in a file, you must first open it. To do this in Object Pascal, you can call the FileOpen() function. Its syntax is: int FileOpen(const String FileName, int Mode);
If you have a file that contains values, you can retrieve them. To do this, you can call the FileRead() function. Its syntax appears as: int FileRead(int Handle, const void * Buffer, int Count); The arguments are the same as those of the FileCreate() function. Here is an example of calling it: //--------------------------------------------------------------------------- void __fastcall TfrmEvaluation::btnOpenClick(TObject *Sender) { int iFileHandle; double dRadius = 0.00; iFileHandle = FileOpen(edtFileName->Text, fmOpenRead); FileRead(iFileHandle, &dRadius, sizeof(double)); edtRadius->Text = FloatToStrF(dRadius, ffFixed, 12, 5); btnCalculateClick(Sender); } //--------------------------------------------------------------------------- |
When using a file, it consumes resources. After using the file, you should free the resources it was using. To do this, call the FileClose function. Its syntax appears as: int FileClose(int Handle); Here are exampes:
//--------------------------------------------------------------------------- #include <vcl.h> #include <math.h> #pragma hdrstop #include "Evaluation.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TfrmEvaluation *frmEvaluation; //--------------------------------------------------------------------------- __fastcall TfrmEvaluation::TfrmEvaluation(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TfrmEvaluation::btnCalculateClick(TObject *Sender) { double dRadius = 0.00; try { dRadius = StrToFloat(edtRadius->Text); edtDiameter->Text = FloatToStrF(dRadius * 2, ffFixed, 12, 5); edtCircumference->Text = FloatToStrF(dRadius * 2 * M_PI, ffFixed, 12, 5); edtArea->Text = FloatToStrF(dRadius * dRadius * 4.0 * M_PI, ffFixed, 12, 5); edtVolume->Text = FloatToStrF(dRadius * dRadius * dRadius * 4.0 * M_PI / 3.00, ffFixed, 12, 5); } catch (EConvertError *) { ShowMessage(L"The value you provided for the radius is not acceptable"); } catch(...) { } } //--------------------------------------------------------------------------- void __fastcall TfrmEvaluation::btnSaveClick(TObject *Sender) { int iFileHandle; double dRadius; // Get the value of the radius dRadius = StrToFloat(edtRadius->Text); // Create the file iFileHandle = FileCreate(edtFileName->Text); // Write the value of the radius to the file FileWrite(iFileHandle, &dRadius, sizeof(double)); // Reset the form edtRadius->Text = L""; edtDiameter->Text = L""; edtCircumference->Text = L""; edtArea->Text = L""; edtVolume->Text = L""; // Close the file FileClose(iFileHandle); } //--------------------------------------------------------------------------- void __fastcall TfrmEvaluation::btnOpenClick(TObject *Sender) { int iFileHandle; double dRadius = 0.00; iFileHandle = FileOpen(edtFileName->Text, fmOpenRead); FileRead(iFileHandle, &dRadius, sizeof(double)); edtRadius->Text = FloatToStrF(dRadius, ffFixed, 12, 5); btnCalculateClick(Sender); FileClose(iFileHandle); } //--------------------------------------------------------------------------- |
|
|||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|