Sixth chapter-Document management (i) (1)

Source: Internet
Author: User
Tags numeric numeric value variables reset

6.1 File types and standard procedures

Delphi, like Object Pascal, supports three types of files, namely, text files, record files, and files of no type.

6.1.1 text File

Variables of a text file type are declared with the following method:

Var

Textfilevar:text;

Text files are read and written in a unit of behavior. Because each row is not necessarily the same length, the exact position of the given row in the file cannot be computed, and can only be read and written sequentially. and text files can only be opened for reading or writing, and it is not allowed to read and write at the same time on an open text file.

6.1.1.1 text File Open, close

The opening of a text file requires two steps: (1). The file variable is associated with the file name; (2). Initialize read and write.

The file variable and filename call the AssignFile standard procedure:

AssignFile (Textfilevar, FileName);

FileName can be either a full pathname or simply a filename. For the latter, the system will look in the current directory.

AssignFile is a new function provided by Delphi, whose function is equivalent to the Assign in Object Pascal. and assign is used more as a method name in Delphi.

There are three ways of initializing read and write:

1. Reset: Open the file for reading and move the file pointer to the top of the file;

2. Rewrite: Create a new document for writing;

3. Append: Open the existing file for writing and position the file pointer at the end of the file.

An I/O exception is thrown when a reset or append procedure is used and the file does not exist. For handling I/O exceptions, see the examples in this chapter and the introduction in chapter 12th.

The closure of the file is simple, just call the CloseFile procedure.

Although the Delphi application automatically closes all open files when it exits, closing the file yourself ensures that the file handle is released and the program is more portable.

To remain compliant, Delphi also allows users to create associations with assign, close the file.

Reading and writing of 6.1.1.2 text files

Reading information from a text file in read, READLN two standard procedures.

When reading values, read, READLN, assumes that values are separated by one or more spaces, rather than commas, semicolons, or other characters. For one of the following statements:

Read (Textfilevar, Num1, Num2, Num3);

If the values in the file are:

100 200 300

Can be read successfully, and if the value in the file is

100 200, 300

When read reads "200," and attempts to convert it to a numeric value, an exception is thrown.

When the read character is a string, the read, READLN procedure always reads as many characters as possible into the string variable or until the line terminator is read. Therefore, reading formatted string data from a text file must declare a string variable that matches its length. If you want to read a word from a file, you must first read each row in the file into a string, and then parse the word one by one from the string. or read only one character from a text file at a time and test each character to see if it is a word disconnect.

The delimiters between the formatted strings should be read into a temporary variable, and the delimiter between the string and the numeric, numeric, and numeric values is automatically recognized as being removed. For the following line of data:

Mon 12:10 40 50

Defined

Var

DAY:STRING[3];

TIME:STRING[5];

NUM1, Num2:integer;

You must read it with the Read statement as follows:

Read (Textfilevar, day, C, time, NUM1, Num2);

C is a temporary character variable.

6.1.1.3 Editing of text files

To implement editing a text file in Delphi, you only need to associate it with a TMemo control:

Memo1.Lines.LoadFromFile (textFileName);

All the changes that are made on the TMemo will be reflected in the file when the SaveToFile method of the memo part is invoked.

6.1.2 Record file

A record file is a file type that is more flexible to operate. It allows for both read and write opening, and can be accessed randomly because the length of each record in the record file is fixed.

The type variable of the record file can be declared as follows:

Var

Recordfilevar:file of RecordType;

RecordType is a custom record type.

For action on logging files we will discuss them in the next section, in conjunction with routines.

6.1.3 file without type

Non-type files provide the underlying I/O channel and can be used to access files with variable length records. Often used in copy operations of files. Because Delphi provides a better approach (see section Fourth), no type files are used infrequently. Interested readers can refer to the two online Help topics in Blockread, Blockwrite.

6.1.4 Delphi Standard process of document management

According to the function we divide the standard process into 11 categories for introduction.

Opening and closing of 6.1.4.1 files

AssignFile: Correlate an external file name with a file variable

Reset: Open a file that exists

Rewrite: Create and open a new file (or overwrite an existing file)

Append: Opens a file as an addition (applies only to text files)

CloseFile: Close an open file

FileOpen: Opens a specific file and returns a handle to the file

Filecreate: Creates a file with a given filename and returns a file handle

FileClose: Closes a file with a specific handle

The three files are used primarily within the system and are often used in the programming of file copying. The objects they manipulate are file handles rather than file variables.

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.