Sixth chapter-Document management (i) (3)

Source: Internet
Author: User
Tags exit exception handling reset

6.2 Application of record files

6.2.1 Task Introduction

In this section, we develop a system security comprehensive assessment method management system. System security is very important in the development of complex projects, but it is difficult to obtain objective and comprehensive evaluation value because of the wide scope. In view of this, we put forward the idea of multiple angles, multiple side evaluation and then quantitative integration, and put forward a variety of security evaluation methods on this basis. Each method is evaluated by different departments and the results are aggregated and synthesized.

For this we define the following record types:

Type

Tnature = (Micro,macro);

{The nature of the method is divided into microscopic and macroscopic two types}

TMethod = Record

NAME:STRING[20]; {Method Name}

CONDITION:STRING[40]; {Method Applicable condition}

Nature:tnature; {Method Nature}

Result:real; {Method estimate value}

End

Information that is used to record different methods.

Because of the different conditions and properties of different methods, the set of methods applied in different stages of engineering development is different. Therefore, the method set should be managed according to the actual situation. We use each method as a record, each method set as a record file. The following is a discussion of how the system is implemented.

Basic idea of 6.2.2 design

The basic function of the system is to open, create, close, display, record increase, modify, delete and the synthesis and display of the result. For this we used two sets of buttons for file and record operations, using a Stringgrid control to display the contents of the file, using a read-only edit box to display the synthesis of the results.

The names and functions of each part are shown in the following table:

Table 6.1 Design of main window parts

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Part name main attribute notes

──────────────────────────────────────

Recfileform Borderstyle=bsdialog file is opened, attach the file name to the window title

Position=poscreencenter

Dynamic determination of STRINGGRID1 size rows

Hazattr (edit box) readonly=true display composite results

Openbutton taborder=0 Open a record file, if the file does not exist, create

Newbutton caption= ' Open ' Create a record file, open if file exists

CloseButton caption= ' Close ' closes an open file

AddButton caption= ' Add ' Add a record

Modifybutton caption= ' modify ' to modify a record

DeleteButton caption= ' Delete ' delete a record

Calcubutton caption= ' Compute ' calculates the final result and displays

Exitbutton caption= ' exit ' system terminated. If you currently have open files, close them first.

OpenDialog1 filter= Select or enter the file you want to open

' Record File (*. REC) |. Rec

| Any File (*.*) |*.* '

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

In addition, the titles of STRINGGRID1 and Hazattr are displayed with two label boxes (labels).

In addition, we need an edit dialog box. Of these, four edit boxes name, Condition, nature, result correspond to four fields of TMethod Records respectively.

To run the coordinator, we define a set of global variables. The types of variables, the role of the following table.

Table 6.2 Global variables and their effects

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Variable name Type action

─────────────────────────────────

Methodfile methodfiletype The file variable associated with the currently open file

FileName string[70] filename of the currently open file

Count Count Total records of currently open files

Currentrec Integer currently processing record number

Fileopened Boolean currently has files open

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The record file type Methodfiletype is defined as

Type

Methodfiletype = file of TMethod;

The Boolean variable fileopened is used to control the ability of the file buttons, to turn gray, to record the response of the button, and to close the file first when the system ends.

Opening and creating of 6.2.3 record files

The opening and creation of a record file requires an association and initialization of two steps as well as a text file. The only difference with a text file is that you cannot use the Append procedure.

The record file is opened read-write by default, and if you want to open it in read-only or write-only mode, you need to modify the value of the variable FileMode defined in the system unit.

FileMode values and meanings are shown in the following table.

Value and significance of table 6.3 FileMode

━━━━━━━━━━━━━━

Value-taking significance

──────────────

0 Read-only

1 Write only

2 Reading and writing

━━━━━━━━━━━━━━

FileMode is a global variable, and every modification to it affects all reset operations, so you should restore its value after you open your own file.

In this system, when the user presses the "open" button, first pops up a standard File Open dialog box, asking the user to enter or select a filename. If the file name of the file exists, it is opened with reset and created if it does not exist. The list of procedures is as follows.

Procedure Trecfileform.openbuttonclick (Sender:tobject);

Begin

If Opendialog1.execute Then

FileName: = Opendialog1.filename

Else

Exit

AssignFile (Methodfile,filename);

Try

Reset (Methodfile);

fileopened: = True;

Except

On Einouterror do

Begin

Try

If FileExists (FileName) = False Then

Begin

ReWrite (Methodfile);

fileopened: = True;

End

Else

Begin

fileopened: = False;

Messagedlg (' Documents cannot be opened ', mtwarning,[mbok],0);

End

Except

On Einouterror do

Begin

fileopened: = False;

Messagedlg (' file cannot be created ', mtwarning,[mbok],0);

End

End

End

End

If fileopened = False then exit;

Count: = FileSize (Methodfile);

If Count>0 Then

Changegrid;

Recfileform.caption: = formcaption+ '--' +filename;

newbutton.enabled: = False;

openbutton.enabled: = False;

closebutton.enabled: = True;

End

First, the system attempts to open a file with reset and set fileopened to True. If the file cannot be opened, an I/O exception is thrown. During exception handling, the first test is whether the file exists. Create this file if it does not exist. Otherwise, the exception that is thrown by the other reason resets the fileopend to false and displays the information "file cannot be opened." An exception can still be thrown during file creation, resetting the fileopened to False in a nested exception handler and prompting the information "file cannot be created."

Please refer to chapter 12th for the contents of exception handling. This program illustrates that the exception handling mechanism not only makes our programs more robust, but also provides flexibility for programming.

When the user presses the "create" button, the system first pops up a standard input box, asks the user to enter the filename, confirms that the system first detects whether the file exists. If it exists, it opens directly, or a new file is created. When opening or creating a procedure causes an exception, the filename and fileopened two global variables are reset.

Procedure Trecfileform.newbuttonclick (Sender:tobject);

Begin

FileName: = InputBox (' Input box ', ' Please enter filename ', ');

If FileName = ' then Exit;

Try

AssignFile (Methodfile,filename);

If FileExists (FileName) Then

Begin

Reset (Methodfile);

Count: = FileSize (Methodfile);

If Count>0 Then

Changegrid;

End

Else

Begin

Rewrite (Methodfile);

Count: = 0;

End

Fileopened: = true;

Except

On Einouterror do

Begin

FileName: = ';

fileopened: = False;

End

End

If Fileopened Then

Begin

newbutton.enabled: = False;

openbutton.enabled: = False;

closebutton.enabled: = True;

Recfileform.caption: = formcaption+ '--' +filename;

End

End

When a file is opened or created, the work to do is:

If the file is not empty, the file length is computed and the file content is populated STRINGGRID1

The "Create", "open" button is dimmed, and the "Close" button enables

Attach the file name to the window title

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.