Current location - Education and Training Encyclopedia - Graduation thesis - Paper data structure
Paper data structure
In DOS operation, the file we are talking about is called an external file. External files are stored on external devices, such as external storage, which can be managed by the computer operating system, such as directly manipulating files with commands such as dir and type.

The file Pascal said is called an internal file. The characteristic of the internal file is that the entity (actual file) of the file is also stored in the external memory and becomes a part of the external file. However, in use, some statements in the program must be linked with the actual file to establish a one-to-one correspondence, and the actual file must be operated with the logical name of the internal file. The logical names of internal files must conform to the naming rules of PASCAL language identifiers.

The files in Pascal are mainly used to store a large amount of data. For example: grade management, there are a lot of original data, which is more convenient and effective than not using files.

A file in Pascal is defined as a linear sequence of elements of the same type. The elements in the file are arranged in a certain order, and each element can be accessed from beginning to end. By definition, files are similar to arrays, but there are obvious differences between them, mainly as follows:

Each element of the (1) file is sequentially stored on an external file device (such as a disk). Therefore, files can be generated by Pascal programs or text editing software, such as edit, ws, Turbo Pascal's edit command, etc. It is generated by the program before the program is executed, or during the running process, and is still stored on the external device after the running.

(2) In the system, access to files is managed through file pointers. A file pointer is a calculator that records the location of a program in a file. At a fixed time, the program can only read or write one element in the file. After an element is written to or read from a file, the corresponding file pointer moves to the next element position. While arrays are accessed by subscripts.

(3) In the definition of file type, there is no need to specify the length of the file, that is, the number of elements, which means that the data of elements can be dynamically changed. A file can be very large and contain many elements, or it can be an empty file without any elements. The number of elements in an array is fixed.

There are roughly the following steps in using files;

(1) Explain the file type and define the file identifier;

(2) Establish the connection between internal documents and external documents;

(3) open the file;

(4) operation documents;

(5) Close the file.

Turbo Pascal classifies files into three categories: text files (sequential), typed files (sequential or random) and untyped files (sequential or random). These files and their operations will be described below.

First, the text file

Text file, also known as text file or text file, can be directly read by people and is one of the basic data forms of human-computer communication. Text files can be directly created, read and modified by text editing programs (such as DOS edit or Turbo PASCAL edit commands), or created by PASCAL programs during operation.

1, definition of text file:

Text file is text, which is composed of ASCII characters and is one of the standard files provided by Pascal. Pascal describes the standard document text as follows:

TYPE TEXT = the character of the file;

Therefore, the text is like the standard type INTEGER, READ, etc. , which can be directly used for variable description without user's explanation. For example:

VAR F 1,F2:TEXT;

Two text file variables F 1 and F2 are defined here.

2, the establishment of the text file

There are two ways to create a text file:

(1) Create the original data file directly with Turbo Pascal editing.

Example 1 Store the data in the following table in a file named A.dat

3 4

29 30 50 60

80 90 70 75

60 50 70 45

Operating steps:

① Enter the editing state of Turbo Pascal;

② Input data;

(3) save, the file name is A.dat

At this point, the data has been stored in the text file A.dat, and the text file can also be established by software such as Edit in DOS.

(2) Establish intermediate data or result data files through programs.

The procedure for creating a file is as follows:

(1) Defines a text file variable;

(2) assigning an external file name to a text file variable to associate the text file with a corresponding external file;

Command format: ASSIGN(f, name)

F is a defined text file variable.

Name is the actual file name.

For example: assign (f 1,' file in.dat)

Or: assignment (f 1,' pas \ file in.res)

In this way, the operation of the text file variable F 1 in the program is also the operation of the external actual file FILEIN. In the above example, the file FILEIN. "DAT" is stored in the current directory, and the file. "RES" is stored in the PAS subdirectory.

③ Open the text file and prepare to write;

Command format 1: rewrite (f)

Function: Create and open a new file for writing. If an existing file with the same name exists, please delete it and recreate it.

Command format 2: Add (f)

Function: Open an existing file and append it.

4 write files;

Command format: WRITE(f,)

Or: WRITELN(f,)

Function: Write the project contents to the F file.

(5) After the file operation, close the file.

Command format: close (f)

Example 2 reads the data of table 12. 1 from the keyboard and writes a file named B.dat with the program.

3. Read the text file

To read the contents of a text file:

(1) Defines a text file variable;

② Use the ASSIGN(f, name) command to link the internal file f with the actual file name;

③ Open the text file for reading;

Command format: READ(f,; ) READLN(f, < variable list >; )

Function: read the data pointed by the pointer in the F file into a variable.

The text file provides two other commands, which are very useful in text operations. They are:

Farewell to Terminator

EOF(f): echo file terminator

⑤ After the file operation is completed, use the Close (F) command to close the file.

Example 3 Read out the text file created in Example 12. 1 and output it.

Because the text file is stored in ASCII code, it is very convenient to view the contents of the text file. In DOS state, you can use commands such as TYPE in DOS, and in Turbo Pascal, you can take files out as programs to view.

4. The characteristics of text files

(1) line structure

A text file consists of several lines, separated by line end tags, with an end tag at the end of the file. Because the length of each line may be different, it is impossible to calculate the exact position of a given line in the text file, so the text file can only be processed sequentially, and one text file cannot be input and output at the same time.

(2) Automatic conversion function

Every element of a text file is of character type, but when a file element is read into a variable (integer, real number or string type), Pascal will automatically convert it into the same data type as the variable. Conversely, when a variable is written to a text file, it will also be automatically converted to a character type.

There are 65,438+00 people in a research group. They take an exam, evaluate six subjects, count the total score of each person and the average score of each subject, and put the original data and the result data into a text file.

analyse

(1) original data file TESTIN. Use Turbo Pascal to edit and create DAT and store it in disk. Its contents are as follows:

10 6

1 78 89 67 90 98 67

2 90 93 86 84 86 93

3 93 85 78 89 78 98

4 67 89 76 67 98 74

5 83 75 92 78 89 74

6 76 57 89 84 73 7 1

7 8 1 93 74 76 78 86

8 68 83 9 1 83 78 89

9 63 7 1 83 94 78 95

10 78 99 90 80 86 70

(2) The program reads in the original data file to find out the total score of each person and the average score of each door;

(3) Create a result data file named TEXTIN. Resolution

Procedure:

Example 5 Read a variable-length text file. Typesetting: Create a file with a fixed length of 60 characters. Typesetting requirements: (1) When the end of a line is not a complete word, replace the last character position of the line with'-',that is, form a complete word with the head of the next line; (2) The header of the first line is two spaces, and the headers of other lines do not contain spaces.

analyse

(1) Create the original data file.

(2) The program reads the contents of the original data file while typesetting.

(3) Each line has 60 characters, which meets the typesetting conditions in the topic and is written into the target file.

Set a text copy of the original data. DAT file is as follows:

Pavel was arrested.

The mother didn't make a fire.

Night came and the cold wind blew.

Someone knocked at the window.

And then the other one.

Mother is used to this kind of beating, but this time she began to be a little happy.

She put on a scarf and opened the door.

Procedure:

Run the program on a copy of the text. DAT text file to obtain typesetting result file TEXTCOPY. RES content is as follows:

Pavel was arrested. The mother didn't light the stove-

Night came and the cold wind blew. Someone knocked at the door.

In the window. And then the other one. Mother is used to knocking at the door like this, b-

But this time she began to be a little happy. Throw a shawl

She turned around and opened the door.

Second, there are many types of files.

All elements in the text file are font characters. To store mixed data in a file, you must use a typed file.

1. Definition of typed files

Elements in typed files can be mixed and stored in binary format, so typed files (except character files, because they are essentially text files) cannot be read and processed by editing software like text files.

The type description format of typed files is:

Type identifier = = file of basic type;

Where the basic type can be any type except file type. For example:

FILE 1 = integer file;

FILE2 = file of string array [1-10];

FILE3 = character set file;

FILE4 = real file;

File 5 = record file;

Name: string;

Course: array of READ [1-10];

Sun: Reading;

End;

And so on, where the types of arrays, sets and records in files 2, 3 and 5 can be described before defining file variables.

For example:

Value-added resale company

f 1:FILE;

F2,F3:file 3;

F4: File 5;

Like all the previous type descriptions and variable definitions, file type descriptions and variable definitions can also be merged, for example:

Value-added resale company

F 1: integer file;

F2, F3: character set file;

F4: Record file

Name: string;

Course: array of real numbers [1-10];

And: reading;

End;

Turbo Pascal can access typed files in a sequential and random way.

To randomly access typed files, Turbo Pascal provides the following commands:

Command format 1: seek (f, n)

Function: Moves the current pointer to the nth component of the specified f file, where f is a non-text file and n is a long integer.

Command format 2: filepos (f)

Function: Returns the current file pointer, which is returned at the file header. The function value is a long integer.

Command Format 3: File Size (F)

Function: Returns the length of the file. If the file is empty, it returns zero, and the function value is a long integer.

2. Create typing files

Typed files can only be created by programs, and the operation steps are similar to those of text files, but the differences are as follows: (1) Typed files have different definitions from text files; (2) Use the SEEK command to specify a pointer, which can be randomly written into typed files.

3. Access the typed file

The operation steps of accessing typed files are similar to those of accessing text files, but the differences are as follows: (1) The definition of typed files is different from that of text files; (2) Typed files can use SEEK command to access any record and any element in the record.

Example 6: Establish typing files of several students' name order, seat number and total scores of six courses.

Analysis: for simplicity, suppose there is a text file FILEDATA. TXT, its content is as follows:

10

Li hong

1 89 67 56 98 76 45

Wang Ming

2 99 87 98 96 95 84

Zhang Yihong

3 78 69 68 69 9 1 8 1

Chang hong

4 8 1 93 82 93 75 76

Lin xing

5 78 65 90 79 89 90

Luo Ze

6 96 85 76 68 69 9 1

Lin Jinjin

7 86 8 1 72 74 95 96

Wang Zheng

8 92 84 78 89 75 97

Maoling

9 84 86 92 86 69 89

Yi Cheng

10 86 94 8 1 94 86 87

The first number 10 means there are 10 students, followed by the first student's name, seat number, six grades, then the second student, and so on.

Read data from a text file, find out everyone's total score, and create a typed file with the file name filedata.fil and the file type record studreco, as shown in the following example.

Procedure:

In Example 7, the generated square, cubic and quartic tables of the number 1- 16 are stored in a typed file, accessed once in a sequential manner, and the two numbers 1 1 and 15 and their corresponding square, cubic and quartic values in the file are accessed in a random manner.

Analysis: The file name of the typed file is BIAO. FIL, and the file type is real.

(1) Generate the number 1- 16 and its square, cubic and quartic values, and write them in the table. FIL, and read out the output in sequence;

(2) use SEEK pointer to point to the positions of files numbered 1 1 and 15, which are 10×4 and 14×4 respectively (note that the first position of the file is 0), and read out the values and the corresponding squares, cubes and fours.

Procedure:

The running results of the program are as follows:

In addition, Turbo Pascal also provides a third form of files, namely untyped files. Typeless files are low-level I/O channels. If the logical interpretation of byte sequences on disks such as typed files and text files is not considered, the physical storage of data is just some byte sequences. This corresponds to the physical units of the memory one by one. Untyped files use 128 consecutive bytes as records (or components) of input and output operations, and data is directly transmitted between disk files and variables, saving file mitigation area, so it takes up less memory than other files, and is mainly used for directly accessing disk files with any fixed length elements.

The specific operation of untyped files is not introduced here, please refer to related books.

Third, comprehensive case analysis.

Example 8 Establish a round-trip adjacency list for city airplanes. Text file CITY. DAT contains the following contents:

The two numbers n and v in the first line;

N represents the number of cities that can be visited, and n is a positive number <100;

V represents the number of direct flights to be listed below, and V is a positive number <100;

The next n lines are the names of cities, and you can go by plane;

The next V line has two cities on each line, separated by spaces, indicating that there are direct flights between the two cities.

For example, CITY 1 CITY2 means flying from CITY 1 to CITY2 or from CITY2 to CITY 1.

Generate file city. RES, an N×N adjacency list consisting of 0 and 1

The adjacency list is defined as:

analyse

(1) n city names read from the text file city.dat are stored in an array CT;

(2) Read in the city name of the transfer flight on Line V, find the positions L and K of two cities in CT for each line, and establish the adjacency relationship, where lj[l, k]= 1, LJ [K, J] =1;

(3) Write the generated adjacency list into the text file CITY.RES.

Let the content of the city. The data are as follows:

10 20

Fuzhou

Beijing

Force … to do …; (Shanghai) Shanghai

Wuhan

Hong Kong

Tianjin

Shen Yan

Nanzenji

Chansa

Guangzhou

Fuzhou Beijin

Fuzhou Shanghai

Fuzhou Guangzhou

Beijing and Shanghai

Guangzhou Beijin

Wuhan Fuzhou

Shanghai Guangzhou

Hong kong Beijing

Fuzhou Hong Kong

Nanchuan Beijin

Nan chan Tian Jin

Tian Jin Bei Jin

Shanghai chansa

Guangzhou Wuhan

chansa beijin

Wuhan Beijin

Shen Yanbeijin

Shen Yan Tianjin

Shanghai Shen Yan

Guangzhou Shen Yan

Procedure:

Get the content of the city. RES file is as follows:

10

1 Fuzhou

2 Beijing

3 Shanghai

4 Wuhan

5 Hong Kong

6 days gold

7 Shen Yan

8 Nanchan

9 chansa

10 Guangzhou

0 1 1 1 1 0 0 0 0 1

1 0 1 1 1 1 1 1 1 1

1 1 0 0 0 0 1 0 1 1

1 1 0 0 0 0 0 0 0 1

1 1 0 0 0 0 0 0 0 0

0 1 0 0 0 0 1 1 0 0

0 1 1 0 0 1 0 0 0 1

0 1 0 0 0 1 0 0 0 0

0 1 1 0 0 0 0 0 0 0

1 1 1 1 0 0 1 0 0 0

Example 9 Contents of file date. FIL files of example 12.3 are sorted according to the total score.

Analysis:

File sorting is to arrange the components of text files according to certain requirements, so that the files are orderly. There are two kinds of file sorting: internal sorting and external sorting. Internal sorting refers to storing the components of a file in an array, then arranging the array, and finally storing the array in the original file. The external arrangement is different from the internal arrangement. Instead of storing file components in an array, it sorts files directly. Internal sorting is faster than external sorting, but when the file is large, it cannot be transferred to memory. It is more appropriate to use external sorting at this time.

This program uses the SEEK process to achieve external sorting.

Procedure:

exercises

1. Write a program to calculate the number of line end tags in a text file.

2. Calculate the average, maximum and minimum line length of the text file.

3. Text files and documents. DAT, which stores the scores of n students in a certain subject, and converts the scores into histograms and stores them in FILE.RES

For example, the contents of a file. The date is:

five

78 90 87 73 84

Contents of histogram file file. RES is as follows:

five

********

*********

*********

*******

********

4. The bank account file contains the account details of each account: account number, name, address and balance of payments. Write a program, read in the accounts of each account and generate bank account files.

5. The contents of each record in the address book file are: name, address, company, zip code and telephone number. A program is programmed to create address book files in the order of names, which requires that files be created first and then sorted externally in the order of names.