📜  Pascal-文件处理

📅  最后修改于: 2020-11-03 16:20:00             🧑  作者: Mango


Pascal将文件视为一系列组件,这些组件必须是统一类型。文件的类型取决于组件的类型。文件数据类型定义为-

type
file-name = file of base-type;

其中,基本类型指示文件组件的类型。基本类型可以是整数,实数,布尔值,枚举,子范围,记录,数组和集之类的东西,除了另一种文件类型。使用var声明创建文件类型的变量-

var
f1, f2,...: file-name;

以下是定义一些文件类型和文件变量的一些示例-

type
   rfile = file of real;
   ifile = file of integer;
   bfile = file of boolean;
   datafile = file of record
   arrfile = file of array[1..4] of integer;

var
   marks: arrfile;
   studentdata: datafile;
   rainfalldata: rfile;
   tempdata: ifile;
   choices: bfile;

创建和写入文件

让我们写一个程序,为学生的记录创建一个数据文件。它将创建一个名为students.dat的文件,并将一个学生的数据写入其中-

program DataFiles;
type
   StudentRecord = Record
      s_name: String;
      s_addr: String;
      s_batchcode: String;
   end;

var
   Student: StudentRecord;
   f: file of StudentRecord;

begin
   Assign(f,'students.dat');
   Rewrite(f);
   Student.s_name := 'John Smith';
   Student.s_addr := 'United States of America';
   Student.s_batchcode := 'Computer Science';
   Write(f,Student);
   Close(f);
end.

编译并运行后,该程序将在工作目录中创建一个名为students.dat的文件。您可以使用记事本等文本编辑器打开文件,以查看John Smith的数据。

从文件读取

我们刚刚创建并写入了一个名为“ students.dat”的文件。现在,让我们编写一个程序,该程序将从文件中读取学生的数据-

program DataFiles;
type
   StudentRecord = Record
      s_name: String;
      s_addr: String;
      s_batchcode: String;
   end;

var
   Student: StudentRecord;
   f: file of StudentRecord;

begin
   assign(f, 'students.dat');
   reset(f); 
   while not eof(f) do
   
   begin
      read(f,Student);
      writeln('Name: ',Student.s_name);
      writeln('Address: ',Student.s_addr);
      writeln('Batch Code: ', Student.s_batchcode);
   end;
   
   close(f);
end.

编译并执行上述代码后,将产生以下结果-

Name: John Smith
Address: United States of America
Batch Code: Computer Science

文件作为子程序参数

Pascal允许将文件变量用作标准和用户定义的子程序中的参数。以下示例说明了此概念。该程序将创建一个名为rainfall.txt的文件,并存储一些降雨数据。接下来,它打开文件,读取数据并计算平均降雨量。

请注意,如果在子程序中使用文件参数,则必须将其声明为var参数。

program addFiledata;
const
   MAX = 4;
type
   raindata = file of real;

var
   rainfile: raindata;
   filename: string;
procedure writedata(var f: raindata);

var
   data: real;
   i: integer;

begin
   rewrite(f, sizeof(data));
   for i:=1 to MAX do
   
   begin
      writeln('Enter rainfall data: ');
      readln(data);
      write(f, data);
   end;
   
   close(f);
end;

procedure computeAverage(var x: raindata);
var
   d, sum: real;
   average: real;

begin
   reset(x);
   sum:= 0.0;
   while not eof(x) do
   
   begin
      read(x, d);
      sum := sum + d;
   end;
   
   average := sum/MAX;
   close(x);
   writeln('Average Rainfall: ', average:7:2);
end;

begin
   writeln('Enter the File Name: ');
   readln(filename);
   assign(rainfile, filename);
   writedata(rainfile);
   computeAverage(rainfile);
end.

编译并执行上述代码后,将产生以下结果-

Enter the File Name:
rainfall.txt
Enter rainfall data:
34
Enter rainfall data:
45
Enter rainfall data:
56
Enter rainfall data:
78
Average Rainfall: 53.25

文字档案

在Pascal中的文本文件由字符行组成,其中每行以行尾标记结尾。您可以将以下文件声明并定义为-

type
file-name = text;

普通字符文件和文本文件之间的区别在于,文本文件被分成几行,每行以特殊的行尾标记终止,由系统自动插入。以下示例创建并写入一个名为contact.txt的文本文件-

program exText;
var
   filename, data: string;
   myfile: text;

begin
   writeln('Enter the file name: ');
   readln(filename);
   
   assign(myfile, filename);
   rewrite(myfile);
   
   writeln(myfile, 'Note to Students: ');
   writeln(myfile, 'For details information on Pascal Programming');
   writeln(myfile, 'Contact: Tutorials Point');
   writeln('Completed writing'); 
   
   close(myfile);
end.

编译并执行上述代码后,将产生以下结果-

Enter the file name:
contact.txt 
Completed writing

附加到文件

追加到文件意味着写入已具有一些数据的现有文件而不会覆盖该文件。以下程序说明了这一点-

program exAppendfile;
var
   myfile: text;
   info: string;

begin
   assign(myfile, 'contact.txt');
   append(myfile);
   
   writeln('Contact Details');
   writeln('webmaster@tutorialspoint.com');
   close(myfile);
   
   (* let us read from this file *)
   assign(myfile, 'contact.txt');
   reset(myfile);
   while not eof(myfile) do
   
   begin
      readln(myfile, info);
      writeln(info);
   end;
   close(myfile);
end.

编译并执行上述代码后,将产生以下结果-

Contact Details
webmaster@tutorialspoint.com
Note to Students:
For details information on Pascal Programming
Contact: Tutorials Point

文件处理功能

Free Pascal提供以下用于文件处理的功能/过程-

Sr.No. Function Name & Description
1

procedure Append(var t: Text);

Opens a file in append mode

2

procedure Assign(out f: file; const Name:);

Assigns a name to a file

3

procedure Assign(out f: file; p: PChar);

Assigns a name to a file

4

procedure Assign(out f: file; c: Char);

Assigns a name to a file

5

procedure Assign(out f: TypedFile; const Name:);

Assigns a name to a file

6

procedure Assign(out f: TypedFile; p: PChar);

Assigns a name to a file

7

procedure Assign(out f: TypedFile; c: Char);

Assigns a name to a file

8

procedure Assign(out t: Text; const s:);

Assigns a name to a file

9

procedure Assign(out t: Text; p: PChar);

Assigns a name to a file

10

procedure Assign(out t: Text; c: Char);

Assigns a name to a file

11

procedure BlockRead(var f: file; var Buf; count: Int64; var Result: Int64);

Reads data from a file into memory

12

procedure BlockRead(var f: file; var Buf; count: LongInt; var Result: LongInt);

Reads data from a file into memory

13

procedure BlockRead(var f: file; var Buf; count: Cardinal; var Result: Cardinal);

Reads data from a file into memory

14

procedure BlockRead(var f: file; var Buf; count: Word; var Result: Word);

Reads data from a file into memory

15

procedure BlockRead(var f: file; var Buf; count: Word; var Result: Integer);

Reads data from a file into memory

16

procedure BlockRead(var f: file; var Buf; count: Int64);

Reads data from a file into memory

17

procedure BlockWrite(var f: file; const Buf; Count: Int64; var Result: Int64);

Writes data from memory to a file

18

procedure BlockWrite(var f: file; const Buf; Count: LongInt; var Result: LongInt);

Writes data from memory to a file

19

procedure BlockWrite(var f: file; const Buf; Count: Cardinal; var Result: Cardinal);

Writes data from memory to a file

20

procedure BlockWrite(var f: file; const Buf; Count: Word; var Result: Word);

Writes data from memory to a file

21

procedure BlockWrite(var f: file; const Buf; Count: Word; var Result: Integer);

Writes data from memory to a file

22

procedure BlockWrite(var f: file; const Buf; Count: LongInt);

Writes data from memory to a file

23

procedure Close(var f: file);

Closes a file

24

procedure Close(var t: Text);

Closes a file

25

function EOF(var f: file):Boolean;

Checks for end of file

26

function EOF(var t: Text):Boolean;

Checks for end of file

27

function EOF: Boolean;

Checks for end of file

28

function EOLn(var t: Text):Boolean;

Checks for end of line

29

function EOLn: Boolean;

Checks for end of line

30

procedure Erase(var f: file);

Deletes file from disk

31

procedure Erase(var t: Text);

Deletes file from disk

32

function FilePos( var f: file):Int64;

Position in file

33

function FileSize(var f: file):Int64;

Size of file

34

procedure Flush(var t: Text);

Writes file buffers to disk

35

function IOResult: Word;

Returns result of last file IO operation

36

procedure Read(var F: Text; Args: Arguments);

Reads from file into variable

37

procedure Read(Args: Arguments);

Reads from file into variable

38

procedure ReadLn(var F: Text; Args: Arguments);

Reads from file into variable and goto next line

39

procedure ReadLn(Args: Arguments);

Reads from file into variable and goto next line

40

procedure Rename(var f: file; const s:);

Renames file on disk

41

procedure Rename(var f: file; p: PChar);

Renames file on disk

42

procedure Rename(var f: file; c: Char);

Renames file on disk

43

procedure Rename(var t: Text; const s);

Rename file on disk

44

procedure Rename(var t: Text; p: PChar);

Renames file on disk

45

procedure Rename( var t: Text; c: Char);

Renames file on disk

46

procedure Reset(var f: file; l: LongInt);

Opens file for reading

47

procedure Reset(var f: file);

Opens file for reading

48

procedure Reset(var f: TypedFile);

Opens file for reading

49

procedure Reset(var t: Text);

Opens file for reading

50

procedure Rewrite(var f: file; l: LongInt);

Opens file for writing

51

procedure Rewrite(var f: file);

Opens file for writing

52

procedure Rewrite(var f: TypedFile);

Opens file for writing

53

procedure Rewrite(var t: Text);

Opens file for writing

54

procedure Seek(var f: file; Pos: Int64);

Sets file position

55

function SeekEOF(var t: Text):Boolean;

Sets file position to end of file

56

function SeekEOF: Boolean;

Sets file position to end of file

57

function SeekEOLn(var t: Text):Boolean;

Sets file position to end of line

58

function SeekEOLn: Boolean;

Sets file position to end of line

59

procedure SetTextBuf(var f: Text; var Buf);

Sets size of file buffer

60

procedure SetTextBuf(var f: Text; var Buf; Size: SizeInt);

Sets size of file buffer

61

procedure Truncate(var F: file);

Truncate the file at position

62

procedure Write(Args: Arguments);

Writes variable to file

63

procedure Write(var F: Text; Args: Arguments);

Write variable to file

64

procedure Writeln(Args: Arguments);

Writes variable to file and append newline

65

procedure WriteLn(var F: Text; Args: Arguments);

Writes variable to file and append newline