Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Files
Introduction
A file is a series of bits of data that are arranged in a particular way to produce a usable
document. For easy storage, location, and management, the bits are stored on a
medium such as a hard disc, a floppy disc, a compact disc, or any valid and supported
type of storage. When these bits belong to a single but common entity, the group is
referred to as a file. For even greater management, files can be stored in a parent
object called a directory or a folder. Since a file is a unit of storage and it stores
information, it has a size which is the number of bits it contains. To manage it, a file
also has a location also called a path that specifies where and/or how the file can be
retrieved. Also, for better management, a file has attributes that indicate what can be
done on a file or that provide specific information that the programmer or the operating
system can use when dealing with the file.
File processing consists of creating, storing, and/or retrieving the contents of a file from a recogniz
example, it is used to save word-processed files to a hard drive, to store a presentation on floppy
file from a CD-ROM. To perform file processing on VCL applications, you have four main choices, tw
and C++ languages, one or a few classes provided by the Visual Component Library, or use the Win
Characteristics of a File
In order to manage files stored in a computer, each file must be able to provide basic pieces of
itself. This basic information is specified when the file is created but can change during the life time
To create a file, a user must first decide where it would be located: this is a requirement. A file can
root drive. Alternatively, a file can be positioned inside of an existing folder. Based on security set
not be able to create a file just anywhere in the (file system of the) computer. Once the user has d
file would reside, there are various means of creating files that the users are trained to use. When
user must give it a name following the rules of the operating system combined with those of the file
At the time of this writing, the rules for file names were on the MSDN web site at Windows
Development\Windows Base Services\Files and I/O\SDK Documentation\Storage\Storage Ove
Management\Creating, Deleting, and Maintaining Files\Naming a File (because it is a web site
its pages can change anytime).
The most fundamental piece of information a file must have is a name. Once the user has created
file is empty or not, the operating system assigns basic pieces of information to it. Once a file is
opened, updated, modified, renamed, etc.
To use a standard Windows dialog box, from the Dialogs tab of the Tool Palette, click the desired d
click anywhere on the form. The position of the control on the form has no importance bec
representative. It will not appear when the form is running. Once the desired dialog’s icon is on
button that will be used to call the dialog. A dialog is called using the DialogName->Execute() met
out what button the user clicked when closing the dialog, and act accordingly.
The primary role of the Save As dialog box is to allow users to store a file on the hard drive of th
portable media such as a floppy disk, or on a network drive. To make this efficient and comple
supply two valuable pieces of information: the location and the name of the file. The location of a
as its path.
The name of a file follows the directives of the operating system. On MS DOS and Windows 3.X,
8.3 format. The actual name had to have a maximum of 8 characters with restrictions on the cha
be used. The user also had to specify three characters after a period. The three characters,
extension, were used by the operating system to classify the file. That was all necessary for thos
operating systems.
Various rules have changed. For example, the names of folders and files on Microsoft Windows >=
255 characters. The extension of the file is mostly left to the judgment of the programmer but the
extensions. Applications can also be configured to save different types of files; that is, files with dif
To use the Save As dialog box, users usually click an item under the File menu. Here is how it work
applications. The user creates a new file. If the user wants to save the file, she can click File -> Sa
not previously saved, the application would call the Save As dialog box. If a file is displaying, whe
previously or not, the user can also click File -> Save As... which also would call the Save As dialog
Two objects are particularly important on the Save As dialog box: The Save In combo box and t
box or combo box (the File Name box is made of a combo box to make it user-friendly but over all
the list side of this combo box). Since Windows 95, the user does not have to specify an
programmer makes it easy. To help with this, the Save As dialog box is equipped with a Save As
This combo box allows the user to select one of the extensions. The available extensions have to
programmer so the user can select from this preset list. If the programmer neglects this, the us
extension to select from. Although the file can still be saved, the operating system would not a
known type of file. Therefore, if you specify a series of extensions, the user can select one of the
Name box, she can simply type a name for the file. If the user does not specify an extension, the
would allocate the extension of the Save As Type combo box. Users of regular commercial applicati
processors, spreadsheet programs, or databases, etc, are usually trained not to care about the e
the application deal with that detail. In some other circumstances, the users must pay close
extension they give a file (this is common on web development or gr
After working on a Save As dialog box, the user can click Save or press Enter, which would valida
change her mind, regardless of what she did on the Save As dialog box, she can click Cancel or
would dismiss the dialog box and ignore what she did (in reality, some actions cannot be ignored,
new file or folder inside of the Save As dialog box, deleting, cutting, or pasting files, etc; but i
Cancel or pressed Esc, the new file would not be saved).
to your application, on the Dialogs property page of the Tool Palette, you can click the SaveDialo
click on a form.
Alternatively, if you cannot add a SaveDialog control at design time, you can create one at run tim
it in an event or a function. If you want the dialog box to be accessible to more than one event or
declare a pointer to a TSaveDialog class. Here is an example:
private:
AnsiString CurrentFile;
TSaveDialog * dlgSave; // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
To make the control available to the form, you can initialize it in the constructor of the form as follo
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
dlgSave = new TSaveDialog(Form1);
}
//---------------------------------------------------------------------------
Eventually, when the form closes, you can make sure the memory occupied by the control is free
dynamic control. This can be done in the OnDestroy event of the form:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete dlgSave;
dlgSave = NULL;
}
//---------------------------------------------------------------------------
To create a list of allowable extensions for your SaveDialog object, use the Filter property
Inspector. At run time, you can create a list of file extensions as a string. If the Save dialog box
extension, you can create the string using the following syntax:
Prompt|Extension
The Prompt is a section that defines what the user would see in the Save As Type combo box. An
24-bit Bitmap. Such a string does not let the user know what actual extension the file would us
courtesy, you can specify, between parentheses, the extension that would be applied if this e
Therefore, the Prompt can be 24-bit Bitmap (*.bmp). In this case, the extension used would be bm
lets the user know that whatever is provided as the file name would be used in place of the as
indicates the separation from the file to its extension. This means that the characters on the left o
be the file name, the characters on the right side of the period would be used as the actual file exte
To specify the extension that the operating system would use to associate to the file, you provide
the string as Extension. In Microsoft Windows, most extensions are made of three characters. Som
a 2-letter extensions (for example Perl files have a pl extension) and some others use 4 letters
some HTML files). This depends on the programmer (or the company that is publishing the applica
of a string the species an extension is:
If you want to provide various extensions to your Save dialog box, you can separate them wit
example would be:
To make the extensions available to the SaveDialog control, you can assign the string to the Filter
an example:
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
dlgSave = new TSaveDialog(Form1);
dlgSave->Filter = "HTML Files (*.htm)|*.htm|"
"Active Server Pages (*.asp)|*.asp|"
"Apache Files (*.php)|*.php|"
"Perl Script (*.pl)|*.pl|"
"All Files";
}
//---------------------------------------------------------------------------
To specify the default extension for your SaveDialog object, type the desired extension in the Defa
Object Inspector.
If you had created a Filter and if you provide a default extension for a SaveDialog object, mak
of the file extensions specified in the Filter list.
Microsoft Windows operating systems, especially since Windows 9X, are configured to have a defa
users are most likely to save their files. On Windows 9X, it is C:\My Documents. On Windows
specified by the network administrator. On Windows 2000 and Windows XP, it uses a more cus
These settings are known to the operating system and you will usually not be concerned with
circumstance, if you want to specify in which folder the users should save their files by default, yo
the InitialDir property. This directory usually ends with \My Documents. If you want to find th
Documents for a user, you can call the SHGetFolderPath(). Its syntax is:
HRESULT SHGetFolderPath(
HWND hwndOwner,
int nFolder,
HANDLE hToken,
DWORD dwFlags,
LPTSTR pszPath
);
Here is an example:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <shfolder.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char strBuffer[MAX_PATH];
SHGetFolderPath(NULL,
CSIDL_PERSONAL,
NULL,
NULL,
strBuffer);
Edit1->Text = strBuffer;
}
//---------------------------------------------------------------------------
Once again, most of the time, you will not be concerned with this issue if you are creating an a
user.
Probably the most important issue users care about, as far as they are concerned, is a name fo
trying to save. Users know that they can set the name of the file in the File Name box. To make t
faster, you can provide a default name for a file in case a user does not want to specify a file nam
typing a name in the FileName field of the Object Inspector. In practicality, the FileName value
displays in the File Name box of the Save As dialog box.
Introduction
One of the most usual involvements with computer files consists of opening them for review or for
the user judges appropriate. Microsoft Windows provides a convenient dialog box to handle the op
job is performed by using the Open File dialog box:
If you prefer to dynamically create an Open dialog box, declare a pointer to TOpenDialog and use
to call its constructor and specify its owner. The technique is the same we applied for the TSaveD
is an example:
Like the SaveDialog control, the default extension is the one the dialog box would first filter duri
you want the Open File dialog to easily recognize a default type of file when the dialog box open
the extension's type using the DefaultExt property.
For convenience, or for security reasons, Open File dialog boxes of applications are sometimes ask
files in a specific location when the Open File dialog box comes up. This default folder is sp
InitialDir property.
The essence of using the Open File dialog box is to be able to open a file. This job is handled b
method which is easily called using a pointer to the OpenDialog object you are using.
5. Click OK
6. Click Title, type Open an Existing Document and press Enter
7. Click an unoccupied area on the form to select it and, in the Object Inspector, click the Events
8. Double-click the OnMouseDown field and implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if( Button == mbRight )
{
if( OpenDialog1->Execute() == True )
{
ShowMessage("The Open button was clicked or the Enter key was pr
"\nThe " + OpenDialog1->FileName +
" file would have been opened.");
}
else
ShowMessage("The Cancel button was clicked or Esc was pressed");
}
}
//---------------------------------------------------------------------------
9. Test the application:
10. After using it, close it and return to Borland C++ Builder
Introduction
The Visual Component Library ships with other dialog boxes useful for tasks that involve files, s
user browse the hard drive to select a folder. This can be done using the Browse For Folder dialog b
The VCL provides a convenient dialog box used to browse the drives of the user’s computer or a
network to locate a folder or a mapped drive.
Creation of a Browse for Folder Dialog Box
The Browse For Folder dialog box is made available through the SelectDirectory() function. Its sy
This function takes three arguments and returns two values. The Caption parameter displays unde
above the tree view of the dialog box. The Root value is a string that represents the name of th
Directory string is the default path folder. This function returns a Boolean value upon exiting. Here
using the SelectDiretory() function:
//---------------------------------------------------------------------------
void __fastcall TForm1::btnDirectoryClick(TObject *Sender)
{
AnsiString Caption = "Select a Directory";
const WideString Root = "C:\"";
AnsiString Directory = "C:\\Program Files";
SelectDirectory(Caption, Root, Directory);
}
//---------------------------------------------------------------------------
When the dialog box opens, it displays two buttons: OK and Cancel. The OK button is disabled bec
would have been selected. To use the Browse For Folder dialog box, the user clicks the + button to
and the – (if any) button to collapse a folder. Once the user locates the desired folder, he must clic
which enables the OK button.
After using the Browse For Folder dialog box, if the user clicks Cancel or presses Esc, whatever
would be dismissed and the function would return false. If the user clicks OK or presses Enter, t
return the full path of the folder the user had selected. This path is the returned Directory argume
conditional statement to find out what button the user had clicked then use the returned Directory
fit. Here is an example:
//---------------------------------------------------------------------------
void __fastcall TForm1::btnDirectoryClick(TObject *Sender)
{
AnsiString Caption = "Select a Directory";
const WideString Root = "C:\"";
AnsiString Directory = "C:\\Program Files";
Introduction
An overloaded version of the SelectDirectory() function allows performing a different type of folde
Creation of the Select Directory Dialog Box
To make available a Select Directory dialog box to your application, you can call the SelectDir
using the following syntax:
Like the other SelectDirectory() function, this version returns two values: a Boolean type and a s
the Directory argument is required, although it is used as a sample that the user can change. Sinc
not a directory, you cannot set its value as “C:”, “C:\””, “A:”, “A:\”” or the likes. The Directory valu
path of an existing folder. When the dialog displays, the string of the Directory is selected, which e
the Cancel buttons. Here is an example:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString Directory = "F:\\Corel\\WordPerfect Office 2002\\Graphics";
SelectDirectory(Directory, TSelectDirOpts(), 0);
}
//---------------------------------------------------------------------------
Once again, the user can change the displaying folder. This time, to proceed, the user would doub
the Directories tree view to expand the folder. Once the user has located the desired folder, h
highlight it. After selecting a folder, the user would click OK. In this case the function would return
of the folder. Two options not available on the first CreateDirectory() version can be used her
display the Directory Name edit box, set the TSelectDirOpts option set to at least:
If the user wants to use a directory that does not exist, if the directory can be created, add the
option to the set as follows:
If the user types a directory that does not exist, it would be created transparently. If you want th
that he wants to create a new folder, add the sdPrompt option to the set. Here is an example:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString Directory = "C:\\Program Files\\Borland\\Delphi4\\Projects";
SelectDirectory(Directory, TSelectDirOpts() << sdAllowCreate
<< sdPerformCreate << sdPrompt, 0);
}
//---------------------------------------------------------------------------
The last option allows you to specify a help file to provide context sensitive help to the user. To u
integer that is mapped to the appropriate identifier in the Help file. Once you set this argument a
has a help file, a Help button would appear on the dialog box. Here is an example of using the help
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString Directory = "C:\\Program Files\\Borland\\Delphi4\\Projects";
const int HelpMe = 12;
SelectDirectory(Directory, TSelectDirOpts() << sdAllowCreate
<< sdPerformCreate << sdPrompt, HelpMe);
}
//---------------------------------------------------------------------------
To directly send a document to the printer, you need to make sure that the control, whose v
printed, supports printing. To accommodate the users of such an application, you can provide a
button they would click. An example of such a button would be . To print, the user can click this b
case when using WordPad; its Standard toolbar is equipped with such a button. With this type of p
user decides to print, the whole document would be printed "as is", in color if the document is
printer supports colors. If there are more than one printer, the computer would use what is kno
printer.
If you want users to be able to configure or customize the printing process, Microsoft Windows pr
dialog box called Print:
The print dialog box allows a user to select a printer if more than one are available. The user ca
print the whole document, to print a range of pages, or to print a portion of the document th
selected. The user can also decide on the number of copies to print from the document, the rang
selected portion. Furthermore, the user can access the particular characteristics of the selected p
how the printer should perform the job. For example, if the selected printer can print in color and t
color but the user wants to print in black and white, she can specify this using the Properties button
The easiest and fastest way to send a document to the printer, if the control that holds the d
supports printing, is by calling the Print() method. For example, since the TRichEdit class and its
the RichEdit support printing, you can simply call the TRichEdit::Print() method. This method ta
as the name of the document that needs to be printed. The argument can be the name of the file. I
a specific name, you can type anything or specify an empty string. Here is an example:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
RichEdit1->Print("Mother and Father");
}
//---------------------------------------------------------------------------
If you want to allow your users to customize or control their printing, you can provide them wit
box:
In the VCL, the Print dialog box is represented through the TPrintDialog class. To add printing du
your application, on the Dialogs property sheet of the Tool Palette, you can click the PrintDialog b
the
If you want to programmatically provide a Print dialog box, you can declare a pointer to TPrin
compiler will need to know "who" owns the printer. This can be done as follows:
Using a menu item or a toolbar button, you can call the Execute() method of the TPrintDialog cl
function, you should first make sure that the user was able to open the Print dialog box, then
printing. Suppose you have a RichEdit control whose content you want to print and suppose you ha
item called mnuPrint and a PrintDialog control named PrintDialog1, you can perform printing with th
//---------------------------------------------------------------------------
void __fastcall TForm1::mnuPrintClick(TObject *Sender)
{
if( PrintDialog1->Execute() )
RichEdit1->Print("");
}
//---------------------------------------------------------------------------
The TPrintDialog class provides all the options to customize the behavior of the P
One of the first choices people make when printing is whether to print the whole document or just
default, the Print dialog box always allows users to print the document completely. This is repre
radio button. Most other characteristics of the Print dialog box are not singly set. A particular cha
works in connection with another effect of the same Print
When customizing the Print dialog box, you will mostly set its options using the Options property
other related properties. The Options property is a set; this means that it allows you to combin
want. If you want users to be able to select a portion of text and print only the selected portion, yo
the Selection radio button is available. This is done by setting the poSelection option to true. If yo
box to appear with the Selection radio button selected, you can set the PrintRange property
Usually this would not be a standard good idea. If you want this radio button to be selected wh
comes up, you should first make sure that a portion of the document to be printed has been selec
if you are (or the user is) trying to print from a RichEdit control, you can check whether there i
text before displaying the dialog box as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::btnPrinterClick(TObject *Sender)
{
if( RichEdit1->SelLength )
PrintDialog1->PrintRange = prSelection;
else
PrintDialog1->PrintRange = prAllPages;
if( PrintDialog1->Execute() )
RichEdit1->Print("");
}
//---------------------------------------------------------------------------
By default, the range of pages to print from the document is not available to the users. If you wa
able to set the range, you can set the poPageNums option to true. Consequently, on a docu
pages, users can print for example pages from 4 through 8.
When using the Print Setup dialog box, the user must first select a printer. This is usually don
operating system that selects the default printer of the computer that called this dialog box. Oth
more than one printer, the user can change it using the Name combo box.
The options of the Print Setup dialog box depend on the driver of the printer selected in the Name
The Print Setup dialog box allows the user to select a printer, if there is more than one, and
appearance of the paper on which the document would be printed. On the Print Setup, the user c
of the Size combo box and select one of the configured sizes of paper:
If the printer has many trays, as indicated by the driver of the selected printer, the user can select
be used when printing. As it happens, one printer can have only one tray while another printer
more:
If the desired printer is on a network, the user can click the Network button to locate it. He or
option to print the document in Portrait (vertical) or in Landscape (horizontal) position.
Previous Copyright © 2005-2007 Yevol Next