8 - File Processing
8 - File Processing
C File Processing
C How To Process Files
File processing is traditionally performed using the FILE class. In the strict C sense,
FILE is a structure and it is defined in the stdio.h header file. This object is equipped
with variables used to indicate what operation would be performed. To use this
structure, you can first declare an instance of the FILE structure. Here is an example:
FILE *Starter;
After instantiating the structure, you can define what to do with it, using one of the
provided functions. Because FILE was created as a C structure, it does not have
member functions. The functions used to perform its related operations on files are also
declared in the stdio.h header file.
7. From the Standard section of the Tool Palette, add the Labels, one ComboBox, and Edit
controls as follows:
8. Name the Edit and the ComboBox controls after their accompanying labels: edtFirstName,
edtMI, edtLastName, edtDOB, cboGender, edtEnglish, edt2ndLanguage, edtHistory,
edtGeography, edtSciences, and edtSports
9. On the form, click the combo box and, in the Object Inspector, double-click the right field of
its Items property
10. Add three lines as Unknown, Male, and Female
11. Click OK
12. From the Additional tab of the Tool Palette, click the BitBtn control and click in the top
Bevel.
13. Change its properties as follows:
Caption = &New
Glyph = New2
Name = btnNew
14. Double-click the New button and implement its OnClick event as follows:
//-------------------------------------------------------------------
--------
void __fastcall TfrmMain::btnNewClick(TObject *Sender)
{
edtFirstName->Text = "";
edtMI->Text = "";
edtLastName->Text = "";
edtDOB->Text = "";
cboGender->ItemIndex = 0;
edtEnglish->Text = "0.00";
edt2ndLanguage->Text = "0.00";
edtHistory->Text = "0.00";
edtGeography->Text = "0.00";
edtSciences->Text = "0.00";
edtSports->Text = "0.00";
}
//-------------------------------------------------------------------
--------
15. Double-click an unoccupied area on the form to access its OnCreate event and implement it
as follows:
//-------------------------------------------------------------------
--------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
btnNewClick(Sender);
}
//-------------------------------------------------------------------
--------
16. Save the project
The first argument, FileName, must be a valid name of a file. If the user is creating or saving a
new file, you can let him specify the name of the file, following the rules of the operating system.
If the user is opening an existing file, you can make sure the file really exists, retrieve its name
and pass it to the fopen() function.
Because the fopen() function is used to save a new file, to open an existing one, or to save a file
that was only modified, the second argument, Mode, actually allows you to decide what operation
the function will be used to perform. This argument is a short string of one or two characters and
can be one of the following:
Mode Role If the file already exists If the file does not exist
If the operation performed using the fopen() function is successful, the function returns a
pointer to the FILE instance that was declared. The FILE structure is usually used in C and C++
console programs that must conform to console applications. However, when used in VCL
applications, because applications are created in a visual development, you should let the users
use the Save and Open common dialog boxes that they are used to. In this case, if the user is
opening a file, you can pass the FileName member variable of the common dialog box to the
fopen() function. Because the fopen() function takes a pointer to char while the Save and Open
dialog boxes use AnsiString members, you should convert The TOpenDialog::FileName or the
TSaveDialog::FileName to a C string.
After using a file, you should/must close its stream. This is done using the fclose() function. Its
syntax is:
To use this function, you must let it know what instance of the FILE object you are closing.
1. From the Dialogs tab of the Tool Palette, click the OpenDialog button and click
anywhere in the form
2. On the Object Inspector, change its DefaultExt to rcd
3. In its Filter field, type
Student Record (*.rcd)|*.rcd|All Files
4. From the Dialogs tab of the Tool Palette, click the SaveDialog button and click anywhere
in the form
5. On the Object Inspector, change its DefaultExt to rcd and, in its Filter field, type
Student Record (*.rcd)|*.rcd|All Files
6. On the top section of the Main.cpp file, include the stdio library file as follows:
//-------------------------------------------------------------------
--------
#include <vcl.h>
#include <cstdio>
using namespace std;
#pragma hdrstop
#include "Main.h"
//-------------------------------------------------------------------
--------
7. To perform the opening of a record, on the form, double-click the Open button and
implement its OnClick event as follows:
//-------------------------------------------------------------------
--------
void __fastcall TfrmMain::btnOpenClick(TObject *Sender)
{
FILE *FOpen;
if( OpenDialog1->Execute() )
{
FOpen = fopen(OpenDialog1->FileName.c_str(), "r+");
fclose(FOpen);
}
//-------------------------------------------------------------------
--------
8. To perform the saving of a record, on the form, double-click the Save button and implement
its OnClick event as follows:
//-------------------------------------------------------------------
--------
void __fastcall TfrmMain::btnSaveClick(TObject *Sender)
{
FILE *FSave;
if( SaveDialog1->Execute() )
{
FSave = fopen(SaveDialog1->FileName.c_str(), "w");
fclose(FSave);
}
//-------------------------------------------------------------------
--------
9. Save the project
Each one of these functions takes a few arguments depending on how it is used. The first
argument, stream, must be an instance of a FILE structure.
The second argument is a string that specifies how data will be formatted and possibly positioned
in the stream instance.The string typically starts with the % symbol followed by one or more
characters that represents a format. Different formats are used depending on the type of data of
the variable that is being written. You can use one the following characters:
After specifying the format, you can type the name of the variable that is being saved. You can
repeatedly use the fprintf() function for each variable you want to save. If you have opened a
file and want to retrieve data stored from it, you can use the fscanf() or the fwscanf() function.
Their syntaxes are:
The first argument, stream, must be a valid instance of a FILE structure. The second argument,
format, follows the same rules as for the fprintf() and the fwprintf() functions. After typing the
format, type the name of the variable that is being retrieved.
if( OpenDialog1->Execute() )
{
FOpen = fopen(OpenDialog1->FileName.c_str(), "r+");
edtFirstName->Text = FirstName;
edtLastName->Text = LastName;
edtDOB->Text = DOB;
cboGender->ItemIndex = Gender;
edtEnglish->Text = English;
edt2ndLanguage->Text = Language2;
edtHistory->Text = History;
edtGeography->Text = Geography;
edtSciences->Text = Sciences;
edtSports->Text = Sports;
}
fclose(FOpen);
}
//-------------------------------------------------------------------
--------
2. In the Main.cpp file, change the OnClick event of the Save button as follows:
//-------------------------------------------------------------------
--------
void __fastcall TfrmMain::btnSaveClick(TObject *Sender)
{
FILE *FSave;
strcpy(FirstName, edtFirstName->Text.c_str());
strcpy(LastName, edtLastName->Text.c_str());
strcpy(DOB, edtDOB->Text.c_str());
Gender = cboGender->ItemIndex;
English = StrToFloat(edtEnglish->Text);
Language2 = StrToFloat(edt2ndLanguage->Text);
History = StrToFloat(edtHistory->Text);
Geography = StrToFloat(edtGeography->Text);
Sciences = StrToFloat(edtSciences->Text);
Sports = StrToFloat(edtSports->Text);
if( SaveDialog1->Execute() )
{
FSave = fopen(SaveDialog1->FileName.c_str(), "w");
fclose(FSave);
}
//-------------------------------------------------------------------
--------
3. Save and test the application
Overview
File processing in C++ is performed using the fstream class. Unlike the FILE structure, fstream
is a complete C++ class with constructors, a destructor and overloaded operators.
To perform file processing, you can declare an instance of an fstream object. If you do not yet
know the name of the file you want to process, you can use the default constructor.
Unlike the FILE structure, the fstream class provides two distinct classes for file processing. One
is used to write to a file and the other is used to read from a file.
Saving a File
Saving a file consists of writing data to disk. To do this, first declare an instance of the ofstream
class using one of its constructors from the following syntaxes:
The ofstream(const char* FileName, int FileMode) constructor provides a complete mechanism
for creating a file. It does this with the help of its two arguments. The first argument, FileName,
is a string that specifies the name of the file that needs to be saved. The second argument,
FileMode, specifies what kind of operation you want to perform on the file. It can be one of the
following:
Mode Description
If FileName is a new file, data is written to it.
ios::app If FileName already exists and contains data, then it is opened, the
compiler goes to the end of the file and adds the new data to it.
If FileName is a new file, data is written to it and subsequently added to
the end of the file.
ios::ate
If FileName already exists and contains data, then it is opened and data is
written in the current position.
If FileName is a new file, then it gets created fine as an empty file.If
ios::in FileName already exists, then it is opened and its content is made
available for processing.
If FileName is a new file, then it gets created fine as an empty file.
Once/Since it gets created empty, you can write data to it.
If FileName already exists, then it is opened, its content is destroyed, and
ios::out the file becomes as new. Therefore you can create new data to write to it.
Then, if you save the file, which is the main purpose of this mode, the new
content is saved it.*This operation is typically used when you want to save
a file.
If FileName already exists, its content is destroyed and the file becomes
ios::trunc
as new.
If FileName is a new file, the operation fails because it cannot create a
new file.
ios::nocreate
If FileName already exists, then it is opened and its content is made
available for processing.
If FileName is a new file, then it gets created fine.
ios::noreplace If FileName already exists and you try to open it, this operation would fail
because it cannot create a file of the same name in the same location.
Image you have a form with three edit boxes whose job is to get the first name, the last name,
and the age of a student:
Other
Control Name Caption/Text
Properties
Bevel
Label First Name:
Edit edtFirstName
Edit edtLastName
Label Age:
Edit edtAge
Bevel
SaveDialog
//---------------------------------------------------------------------------
#include <vcl.h>
#include <fstream>
using namespace std;
#pragma hdrstop
#include "Exercise.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSaveClick(TObject *Sender)
{
char FirstName[30], LastName[30];
int Age;
strcpy(FirstName, edtFirstName->Text.c_str());
strcpy(LastName, edtLastName->Text.c_str());
Age = edtAge->Text.ToInt();
if( SaveDialog1->Execute() )
{
ofstream Students(SaveDialog1->FileName.c_str(), ios::out);
Students << FirstName << "\n" << LastName << "\n" << Age;
}
}
The default constructor, ofstream(), can be used to create an empty stream if you do not yet
have enough information about the file you intend to deal with and what type of operation you
will perform. This constructor is used if you plan to call member methods to perform the desired
file processing.
After declaring an instance of the ofstream class, you can use the ofstream::open() method to
create the file. The syntax of the open() method is:
This method behaves exactly like, and uses the same arguments as, the constructor we described
above. The first argument represents the name of the file you are dealing with and the FileMode
argument follows the modes of the above table.
Because the fstream class in this case is declared as ofstream, the compiler is aware that you
want to save a file (in reality, the use of ofstream means that you want to write to a file, in
other words you want the FileMode with a value of ios::out), you can use the first constructor
with just the FileName as argument or you can call the open() method with only the name of the
file. Here is an example:
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSaveClick(TObject *Sender)
{
char FirstName[30], LastName[30];
int Age;
strcpy(FirstName, edtFirstName->Text.c_str());
strcpy(LastName, edtLastName->Text.c_str());
Age = edtAge->Text.ToInt();
if( SaveDialog1->Execute() )
{
ofstream Students;
Students.open(SaveDialog1->FileName.c_str());
Students << FirstName << "\n" << LastName << "\n" << Age;
}
}
//---------------------------------------------------------------------------
After using a file, you should close it. This is taken care by using the ofstream::close() method
whose syntax is:
void close();
SaveDialog button
3. While the SaveDialog button is still selected on the form, on the Object Inspector, change
the DefaultExt to btd
4. In the FileName field, type Untitled
5. In the Filter box, type
Body Tag Documents (*.btd)|*.btd|Text Files (*.txt)|*.txt|All Files|*.*
6. In the Title box, type Save Current Format Tag
7. From the Additional tab of the Tool Palette, double-click the BitBtn button
8. Set its Glyph as the Floppy1 bitmap from the Bitmaps folder that accompanies this ebook
9. Change the button’s Name to btnSave and change its Caption to &Save…
10. Double-click the new Save button to access its OnClick() event
11. On the top section of the source file, include the fstream library:
//-------------------------------------------------------------------
--------
#include <vcl.h>
#include <fstream>
using namespace std;
#pragma hdrstop
12. Implement the new event as follows:
//-------------------------------------------------------------------
--------
void __fastcall TfrmMain::btnSaveClick(TObject *Sender)
{
DWORD RedBG, GreenBG, BlueBG,
RedText, GreenText, BlueText,
RedLink, GreenLink, BlueLink,
RedALink, GreenALink, BlueALink,
RedVLink, GreenVLink, BlueVLink;
RedBG = GetRValue(mmoPreview->Color);
GreenBG = GetGValue(mmoPreview->Color);
BlueBG = GetBValue(mmoPreview->Color);
RedText = GetRValue(edtPreviewText->Font->Color);
GreenText = GetGValue(edtPreviewText->Font->Color);
BlueText = GetBValue(edtPreviewText->Font->Color);
RedLink = GetRValue(edtPreviewLink->Font->Color);
GreenLink = GetGValue(edtPreviewLink->Font->Color);
BlueLink = GetBValue(edtPreviewLink->Font->Color);
RedALink = GetRValue(edtPreviewALink->Font->Color);
GreenALink = GetGValue(edtPreviewALink->Font->Color);
BlueALink = GetBValue(edtPreviewALink->Font->Color);
RedVLink = GetRValue(edtPreviewVLink->Font->Color);
GreenVLink = GetGValue(edtPreviewVLink->Font->Color);
BlueVLink = GetBValue(edtPreviewVLink->Font->Color);
if( SaveDialog1->Execute() )
{
ofstream FormatToSave(SaveDialog1->FileName.c_str(),
ios::out);
if( !FormatToSave )
{
ShowMessage("There was a problem saving the file.");
return;
}
FormatToSave.close();
}
}
//-------------------------------------------------------------------
--------
13. Press F9 to test the application
14. To test it, change the colors of the attributes (Background, Text, Link, Active Link, and
Visited Link). Then click Save and set the name to Firmament
15. Close the application and return to Borland C++ Builder
16. Save All
Opening a File
Besides saving, another operation you can perform consists of opening an already existing file to
have access to its contents. To do this, C++ provides the ifstream class. Like ofstream, the
ifstream class provides various constructors you can use, two of which are particularly
important. If you have enough information about the file you want to open, you can use the
following constructor:
The first argument of the constructor, FileName, is a constant string that represents the file that
you want to open. The FileMode argument is a natural number that follows the table of modes as
we described above.
If necessary, you can also declare an empty instance of the ifstream class using the default
constructor:
ifstream();
After declaring this constructor, you can use the ifstream::open() method to formally open the
intended file. The syntax of the open() method is:
This method uses the same arguments as the above constructor. By default, when declaring an
instance of the ifstream class, it is assumed that you want to open a file; that is, you want to
use the FileMode attribute with a value of ios::in. Therefore, the second argument is already set
to ios::in value. This allows you to call the open() method with just the FileName value.
After using the ifstream class, you can close it using the ifstream::close() method. Here is an
example:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <fstream>
using namespace std;
#pragma hdrstop
#include "Exercise.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSaveClick(TObject *Sender)
{
char FirstName[30], LastName[30];
int Age;
strcpy(FirstName, edtFirstName->Text.c_str());
strcpy(LastName, edtLastName->Text.c_str());
Age = edtAge->Text.ToInt();
if( SaveDialog1->Execute() )
{
ofstream Students;
Students.open(SaveDialog1->FileName.c_str());
Students << FirstName << "\n" << LastName << "\n" << Age;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnOpenClick(TObject *Sender)
{
char FirstName[30], LastName[30];
int Age;
if( OpenDialog1->Execute() )
{
ifstream Students;
Students.open(OpenDialog1->FileName.c_str());
Students >> FirstName >> LastName >> Age;
Students.close();
edtFirstName->Text = FirstName;
edtLastName->Text = LastName;
edtAge->Text = Age;
}
}
//---------------------------------------------------------------------------
OpenDialog button
2. While the OpenDialog button is still selected on the form, on the Object Inspector, change
the DefaultExt to btd
3. In the FileName field, type Untitled
4. In the Filter box, type
Body Tag Documents (*.btd)|*.btd|Text Files (*.txt)|*.txt|All Files|*.*
5. In the Title box, type Open a Tag Format
6. From the Additional tab of the Tool Palette, double-click the BitBtn button
7. Set its Glyph as the Open1 bitmap from the Bitmaps folder that accompanes this ebook
8. Change the button’s Name to btnOpen and change its Caption to &Open…
9. Double-click the Open button and implement its OnClick() event as follows:
//-------------------------------------------------------------------
--------
void __fastcall TfrmMain::btnOpenClick(TObject *Sender)
{
DWORD RedBG, GreenBG, BlueBG,
RedText, GreenText, BlueText,
RedLink, GreenLink, BlueLink,
RedALink, GreenALink, BlueALink,
RedVLink, GreenVLink, BlueVLink;
ifstream FormatToOpen;
if( OpenDialog1->Execute() )
{
FormatToOpen.open(OpenDialog1->FileName.c_str(),
ios::in);
if( !FormatToOpen )
{
ShowMessage("There was a problem opening the
file.");
return;
}
FormatToOpen.close();
edtNumRed->Text = RedBG;
edtNumGreen->Text = GreenBG;
edtNumBlue->Text = BlueBG;
edtBackground->Text = "#" +
AnsiString(IntToHex(__int64(RedBG), 2)) +
AnsiString(IntToHex(__int64(GreenBG), 2)) +
AnsiString(IntToHex(__int64(BlueBG), 2));
edtText->Text = "#" +
AnsiString(IntToHex(__int64(RedText), 2)) +
AnsiString(IntToHex(__int64(GreenText), 2)) +
AnsiString(IntToHex(__int64(BlueText), 2));
edtLink->Text = "#" +
AnsiString(IntToHex(__int64(RedLink), 2)) +
AnsiString(IntToHex(__int64(GreenLink), 2)) +
AnsiString(IntToHex(__int64(BlueLink), 2));
edtALink->Text = "#" +
AnsiString(IntToHex(__int64(RedALink), 2)) +
AnsiString(IntToHex(__int64(GreenALink), 2)) +
AnsiString(IntToHex(__int64(BlueALink), 2));
edtVLink->Text = "#" +
AnsiString(IntToHex(__int64(RedVLink), 2)) +
AnsiString(IntToHex(__int64(GreenVLink), 2)) +
AnsiString(IntToHex(__int64(BlueVLink), 2));
edtPreviewText->Font->Color =
TColor(RGB(RedText, GreenText,
BlueText));
edtPreviewLink->Font->Color =
TColor(RGB(RedLink, GreenLink,
BlueLink));
edtPreviewALink->Font->Color =
TColor(RGB(RedALink, GreenALink,
BlueALink));
edtPreviewVLink->Font->Color =
TColor(RGB(RedVLink, GreenVLink,
BlueVLink));
pnlPreview->Color = TColor(RGB(RedBG, GreenBG,
BlueBG));
mmoPreview->Color = TColor(RGB(RedBG, GreenBG,
BlueBG));
edtPreviewText->Color = TColor(RGB(RedBG, GreenBG,
BlueBG));
edtPreviewLink->Color = TColor(RGB(RedBG, GreenBG,
BlueBG));
edtPreviewALink->Color= TColor(RGB(RedBG, GreenBG,
BlueBG));
edtPreviewVLink->Color= TColor(RGB(RedBG, GreenBG,
BlueBG));
grpBodyAttributes->ItemIndex = 0;
}
}
//-------------------------------------------------------------------
--------
10. Press F9 to test the application
11. Open the previously saved format by clicking the Open... button
12. After using the application, close it and return to Borland C++ Builder
Introduction
The Visual Component Library (VCL) provides various built-in classes to perform file processing.
Most of the features are provided through the TFileStream class. To perform file streaming
using this class, first declare its instance using its constructor whose syntax is:
The first argument of this constructor is the name (or path) of the file you are dealing with. If the
file does not exist or it cannot be accessed (opened or saved) for any reason, the compiler would
throw an error and stop the action.
The second action, Mode, specifies what you are trying to do with the file.
The TFileStream class is conceptually designed to deal with the contents of one or more
controls. Therefore, instead of concerning yourself with the values of controls, TFileStream
would consider the change that affect a control on behalf of the user, which mostly is its
contents. When saving a file, TFileStream faithfully gets the contents of all controls as you wish
and saves them in one file. If opening a file, TFileStream locates the content of each file and
restores it. Based on this, TFileStream is appropriate for VCL objects and usually its files should
not mixed with non-VCL controls.
When saving a file, the Mode argument of the constructor can be passed as one of the following
constants:
When it is possible that other people or application would try accessing the same file at the same
time, the following constants can be used to manage the sharing of files:
To combine these two mode for the Mode argument, you use the bitwise OR operator |
Imagine you create a form equipped with a Memo and an Edit controls:
Here is an example of saving the contents of both controls to a file:
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSaveClick(TObject *Sender)
{
// Decalre a pointer to TFileStream
TFileStream *FStream;
When opening a file, you can use one of the following modes as the Mode argument:
Read Mode Description
fmOpenRead This is used to open a file but the user cannot modify and then save it.
fmOpenReadWrite This allows opening an existing file, modifying, and saving it.
You can combine this mode with one of the above share modes using the bitwise OR operator.
Here is an example from the same above form design:
//---------------------------------------------------------------------------
void __fastcall TForm1::btnOpenClick(TObject *Sender)
{
TFileStream *FStream;
if( OpenDialog1->Execute() )
{
try {
FStream = new TFileStream(OpenDialog1->FileName,
fmOpenRead | fmShareExclusive);
FStream->ReadComponent(Memo1);
FStream->ReadComponent(Edit1);
}
__finally
{
delete FStream;
}
}
}
//---------------------------------------------------------------------------
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:
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:
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;
}
}
}
//---------------------------------------------------------------------------
File Creation
In your VCL applications, even on console applications created on the Microsoft Windows
operating system, besides the C, the C++, and the VCL means of saving and opening files, the
Win32 library provides its mechanism of file processing.
In order to use a file, you must obtain a handle for it. You can obtain a file handle by calling the
CreateFile() function. Its syntax is:
HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
The lpFileName argument is a null-terminated string that represents the name of the file. You can
explicitly specify it as a double-quoted string. If you want the user to save the current file or to
open an existing file by the user specifying the name of the file, you can use an OpenDialog or a
SaveDialog objects. In this case the lpFileName can be obtained with the
TOpenDialog::FileName or the TSaveDialog::FileName member variables.
The dwDesiredAccess argument specifies the type of operation that will be performed on the file.
It can have one or a combination of the following values:
0: Information about a device (floppy, CD, DVD, hard disk, etc) will be retrieved without a
file being accessed
DELETE: The file can be deleted
READ_CONTROL: The read rights of the file can be accessed or read from
STANDARD_RIGHTS_EXECUTE: Same as READ_CONTROL
STANDARD_RIGHTS_READ: Same as READ_CONTROL
STANDARD_RIGHTS_WRITE: Same as READ_CONTROL
SYNCHRONIZE: The file can be synchronized
WRITE_DAC: The discretionary access control list (DACL) can be modified
WRITE_OWNER: The owner of the file can be changed
FILE_EXECUTE: The file can be executed. For example, if the file being accessed is an
application, it can be launched
FILE_READ_DATA: The data of the file can be read from
FILE_WRITE_DATA: Data can be written to the file
FILE_APPEND_DATA: data can be added to end of the file
FILE_READ_ATTRIBUTES: The attributes of the file can be accessed or viewed
FILE_READ_EA: The extended attributes of the file can be read from
FILE_WRITE_ATTRIBUTES: The attributes of the file can be modified
FILE_WRITE_EA: The extended attributes of the file can be written to
STANDARD_RIGHTS_READ: The read rights of the file can be read
STANDARD_RIGHTS_WRITE: The write rights of the file can be modified
Omitting the 0 value, the above flags can be combined using the bitwise OR operator.
Alternatively, you can pass one of the following values for a combination of flags:
The dwShareMode argument controls how the file can be shared. Its possible values are:
The lpSecurityAttributes argument specifies some security attributes used when creating the file.
Such attributes are defined as SECURITY_ATTRIBUTES value. You can pass this argument as
NULL, in which case the security attributes would be ignored.
The dwCreationDisposition argument specifies the behavior to adopt whether the file already
exists or is just being created. It can have one of the following values:
CREATE_ALWAYS: If the file does not exist, it will be created. If the file exists already, it
will be destroyed and replaced by a new one with an attribute of Archive
CREATE_NEW: If the file does not exist, it will be created. If the file already exists, the
CreateFile() function would fail
OPEN_ALWAYS: If the file does not exist, it will be created. If the file already exists, it
would be opened
OPEN_EXISTING: If the file does not exist, the CreateFile() function would fail. If the file
already exists, it would be opened
TRUNCATE_EXISTING: If the file does not exist, the CreateFile() function would fail. If
the file already exists, it must be opened with a GENERIC_WRITE dwDesiredAccess flag
and subsequently, its size would be reduced to 0 bytes
The dwFlagsAndAtributes argument specifies the attribute(s) to apply to the file. It can have one
or a combination of the following values:
The above attributes can also be combined with the following values:
FILE_FLAG_BACKUP_SEMANTICS
FILE_FLAG_DELETE_ON_CLOSE
FILE_FLAG_NO_BUFFERING
FILE_FLAG_OPEN_NO_RECALL
FILE_FLAG_OPEN_REPARSE_POINT
FILE_FLAG_OVERLAPPED
FILE_FLAG_POSIX_SEMANTICS
FILE_FLAG_RANDOM_ACCESS
FILE_FLAG_SEQUENTIAL_SCAN
FILE_FLAG_WRITE_THROUGH
The hTemplateFile argument is a handle to a template file. If you do not have or cannot use a file
template (for example they are not supported on home operating systems), pass this argument
as NULL.
If the CreateFile() function succeeds, it returns a handle to the file and you can use it as you
see fit, to analyze it, to read its contents if allowed, or to write data to it if permitted. If this
function fails, it returns INVALID_HANDLE_VALUE.
Edit edtProcessedBy
GroupBox Ingredients
GroupBox Options
Bevel
7. In the header file of the form, create a structure called TCustomerOrder and declare its
instance in the private section of the form’s class:
//-------------------------------------------------------------------
--------
struct TCustomerOrder
{
char strClerk[20];
TDateTime dteOrderDate;
Integer iBread;
Integer iMeat;
Boolean bLettuce;
Boolean bOnion;
Boolean bTomato;
Boolean bPickles;
Integer iIngredients;
Boolean bCheese;
Boolean bBacon;
};
//-------------------------------------------------------------------
--------
class TfrmMain : public TForm
{
__published: // IDE-managed Components
. . .
private:
void __fastcall EvaluatePrice();
TCustomerOrder CustOrder; // User declarations
public: // User declarations
__fastcall TfrmMain(TComponent* Owner);
};
//-------------------------------------------------------------------
--------
8. Save all
9. To add another form, on the main menu, click File -> New -> Form
10. Design the form as follows
Control Name Caption/Text Other Properties
Bevel
File Saving
As its name suggests, the CreateFile() function is used to create a stream. Actually, it initiates a
file object. After calling, since it returns a file handle, your next action is to decide what to do
with this handle and one of the actions you can take is to store a newly created file into a drive.
To save a file in Win32, you can call the WriteFile() function. Of course, in order to save a file,
you must first retrieve the value to be saved. This value can be made of a single variable or a
complex class. Therefore, you must know what the value to save is made of because it must be
supplied to the WriteFile() function. The syntax of this function is:
The hFile argument is the handle to the file. It is typically the return value of a call to the
CreateFile() function. Since in this case we are trying to save a file, the file should/must have
been created with at least the GENERIC_WRITE flag for the dwDesiredAccess argument of the
CreateFile() function.
The lpBuffer argument is the object that will be saved. As you can see, it is defined as a pointer
to VOID, meaning that its type is not known in advance, which leaves it up to you to decide what
type of value is being saved. It can be a C/C++ generic type (integer, character, floating-point
value, or their variants). It can be a VCL or a Win32 type of object. It can also be a completely
new type that you create as a simple C structure or a more elaborate C++, VCL, or Win32 class.
The nNumberOfBytesToWrite argument is the number of bytes to save. As the DWORD type
indicates, it must be a positive integer.
The lpOverlapped argument is necessary only if the file was created with the
FILE_FLAG_OVERLAPPED flag for the dwFlagsAndAtributes argument of the CreateFile()
function.
1. From the Dialogs tab of the Tool Palette, click the SaveDialog button and click on the
form
2. Change its properties as follows:
DefaultExt: fst
Filter: Fast Food Files (*.fst)|*.fst|Text Files (*.txt)|*.txt|All Files
Title: Save Customer's Order
3. On the form, double-click the Save button and implement its OnClick() event as follows:
//-------------------------------------------------------------------
--------
void __fastcall TfrmMain::btnSaveClick(TObject *Sender)
{
HANDLE hFile;
DWORD dFileSize, dBytesWritten;
BOOL bResult;
if( SaveDialog1->Execute() )
{
__try {
hFile = CreateFile(SaveDialog1-
>FileName.c_str(),
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_FLAG_RANDOM_ACCESS, NULL);
dFileSize = sizeof(CustOrder);
strcpy(CustOrder.strClerk, edtClerk-
>Text.c_str());
CustOrder.dteOrderDate = edtOrderDate->Text;
if( rdoBun->Checked )
CustOrder.iBread = 0;
else if( rdoRoll->Checked )
CustOrder.iBread = 1;
if( rdoBeefPatty->Checked )
CustOrder.iMeat = 0;
else if( rdoGrilledChicken->Checked )
CustOrder.iMeat = 1;
else if( rdoChickenBreast->Checked )
CustOrder.iMeat = 2;
CustOrder.bLettuce = dlgIngredients->chkLettuce-
>Checked;
CustOrder.bOnion = dlgIngredients->chkOnion->Checked;
CustOrder.bTomato = dlgIngredients->chkTomato-
>Checked;
CustOrder.bPickles = dlgIngredients->chkPickles-
>Checked;
CustOrder.bCheese = chkCheese->Checked;
CustOrder.bBacon = chkBacon->Checked;
if( rdoMayonnaise->Checked )
CustOrder.iIngredients = 0;
else if( rdoKetchup->Checked )
CustOrder.iIngredients = 1;
else if( rdoMustard->Checked )
CustOrder.iIngredients = 2;