Chapter Sixth document Management (i)

Source: Internet
Author: User

Delphi , like Object Pascal, supports three file types, namely: text files, record files, and untyped files.

6.1.1 text File

Variables for text file types are declared as follows:

Var

Textfilevar:text;

A text file is read and written in the Unit of action. Because the length of each line is not necessarily the same, the exact position of the given row in the file cannot be computed, and thus can be read and written sequentially. Also, 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 Opening and closing of text files

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

linked file variable and file name call AssignFile Standard Process:

AssignFile (Textfilevar, FileName);

FileName can be either a full path name or just a file name. For the latter system will be looked up under the current directory.

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

There are three ways to initialize 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 file for writing;

3. Append: to write open the existing file and position the file pointer at the end of the file.

when using An I/O exception will be thrown when the Reset or Append process and the file does not exist . For the handling of I/O exceptions, see the examples in this chapter and the introduction in chapter 12th.

when using An I/O exception will be thrown when the Reset or Append process and the file does not exist . For the handling of I/O exceptions, see the examples in this chapter and the introduction in chapter 12th.

closing the file is simple, just call CloseFile process can be done.

Although the Delphi application automatically closes all open files when exiting, but closing the file on its own will ensure that the file handle is freed and the program's portability is enhanced.

to maintain compatibility, Delphi also allows users to use Assign to establish associations,close files.

6.1.1.2 text file reading and writing

To read information from a text file. Read,readln two standard procedures.

When a value is read, Read,readln assumes that values are separated by one or more spaces, not commas, semicolons, or other characters. A statement to the following:

Read (Textfilevar, Num1, Num2, Num3);

If the value in the file is:

100 200 300

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

300,

the Read reads "$," and attempts to convert it into a numeric value throws an exception.

When the read-in character is a string, The read,readln process always reads as many characters as possible into a string variable or until the line terminator is read. Therefore, reading formatted string data from a text file, you must declare a string variable that matches its length. If you want to read a word from a file, you must first read each line in the file into the 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 for the break in the word.

The delimiter between the formatted strings should be read into a temporary variable, and the character string and the delimiter between the numeric and numeric values are automatically recognized when they are read. The following line of data:

Mon 12:10 40 50

Defined

Var

DAY:STRING[3];

TIME:STRING[5];

NUM1, Num2:integer;

The following read statement is required :

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

C is a temporary character variable.

6.1.2 Record File

A record file is a more flexible type of file. It allows both read and write open, and is randomly accessed due to the fixed length of each record in the record file.

The type variables for the record file can be declared as follows:

Var

Recordfilevar:file of RecordType;

RecordType is a custom record type.

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

6.1.3 No type file

The untyped file provides the underlying I/O channels, which can be used to access files of variable-length records. Frequently used in copy operations of files. Because Delphi provides a better approach ( see section Fourth ), the untyped file is seldom used. Interested readers can refer to the blockread,blockwrite two online Help topics.

6.1.4 Delphi 's document management standard process

According to the function, we divide the standard process into 11 classes to introduce.

6.1.4.1 Opening and closing of a file

AssignFile : To correlate an external file name with a variable

Reset : Open a file that exists

Rewrite : Create and open a new file (or overwrite the original file)

Append : Open a file as added (only for text files)

CloseFile : Close an open file

FileOpen : Opens a specific file and returns a file handle

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

FileClose : Closing a file with a specific handle

The back three files are intended for internal use by the system and are often used in the programming of file replication. The objects they manipulate are file handles rather than file variables.

Chapter Sixth document Management (i)

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.