
- Pascal 教程
- Pascal - 首頁
- Pascal - 概述
- Pascal - 環境設定
- Pascal - 程式結構
- Pascal - 基本語法
- Pascal - 資料型別
- Pascal - 變數型別
- Pascal - 常量
- Pascal - 運算子
- Pascal - 決策制定
- Pascal - 迴圈
- Pascal - 函式
- Pascal - 過程
- Pascal - 變數作用域
- Pascal - 字串
- Pascal - 布林值
- Pascal - 陣列
- Pascal - 指標
- Pascal - 記錄
- Pascal - 變體
- Pascal - 集合
- Pascal - 檔案處理
- Pascal - 記憶體
- Pascal - 單元
- Pascal - 日期和時間
- Pascal - 物件
- Pascal - 類
- Pascal 有用資源
- Pascal - 快速指南
- Pascal - 有用資源
- Pascal - 討論
Pascal - 檔案處理
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 提供以下檔案處理函式/過程:
序號 | 函式名稱和描述 |
---|---|
1 |
procedure Append(var t: Text); 以追加模式開啟檔案 |
2 |
procedure Assign(out f: file; const Name:); 為檔案分配名稱 |
3 |
procedure Assign(out f: file; p: PChar); 為檔案分配名稱 |
4 |
procedure Assign(out f: file; c: Char); 為檔案分配名稱 |
5 |
procedure Assign(out f: TypedFile; const Name:); 為檔案分配名稱 |
6 |
procedure Assign(out f: TypedFile; p: PChar); 為檔案分配名稱 |
7 |
procedure Assign(out f: TypedFile; c: Char); 為檔案分配名稱 |
8 |
procedure Assign(out t: Text; const s:); 為檔案分配名稱 |
9 |
procedure Assign(out t: Text; p: PChar); 為檔案分配名稱 |
10 |
procedure Assign(out t: Text; c: Char); 為檔案分配名稱 |
11 |
procedure BlockRead(var f: file; var Buf; count: Int64; var Result: Int64); 從檔案讀取資料到記憶體 |
12 |
procedure BlockRead(var f: file; var Buf; count: LongInt; var Result: LongInt); 從檔案讀取資料到記憶體 |
13 |
procedure BlockRead(var f: file; var Buf; count: Cardinal; var Result: Cardinal); 從檔案讀取資料到記憶體 |
14 |
procedure BlockRead(var f: file; var Buf; count: Word; var Result: Word); 從檔案讀取資料到記憶體 |
15 |
procedure BlockRead(var f: file; var Buf; count: Word; var Result: Integer); 從檔案讀取資料到記憶體 |
16 |
procedure BlockRead(var f: file; var Buf; count: Int64); 從檔案讀取資料到記憶體 |
17 |
procedure BlockWrite(var f: file; const Buf; Count: Int64; var Result: Int64); 將資料從記憶體寫入檔案 |
18 |
procedure BlockWrite(var f: file; const Buf; Count: LongInt; var Result: LongInt); 將資料從記憶體寫入檔案 |
19 |
procedure BlockWrite(var f: file; const Buf; Count: Cardinal; var Result: Cardinal); 將資料從記憶體寫入檔案 |
20 |
procedure BlockWrite(var f: file; const Buf; Count: Word; var Result: Word); 將資料從記憶體寫入檔案 |
21 |
procedure BlockWrite(var f: file; const Buf; Count: Word; var Result: Integer); 將資料從記憶體寫入檔案 |
22 |
procedure BlockWrite(var f: file; const Buf; Count: LongInt); 將資料從記憶體寫入檔案 |
23 |
procedure Close(var f: file); 關閉檔案 |
24 |
procedure Close(var t: Text); 關閉檔案 |
25 |
function EOF(var f: file):Boolean; 檢查檔案結尾 |
26 |
function EOF(var t: Text):Boolean; 檢查檔案結尾 |
27 |
function EOF: Boolean; 檢查檔案結尾 |
28 |
function EOLn(var t: Text):Boolean; 檢查行尾 |
29 |
function EOLn: Boolean; 檢查行尾 |
30 |
procedure Erase(var f: file); 從磁碟刪除檔案 |
31 |
procedure Erase(var t: Text); 從磁碟刪除檔案 |
32 |
function FilePos( var f: file):Int64; 檔案中的位置 |
33 |
function FileSize(var f: file):Int64; 檔案大小 |
34 |
procedure Flush(var t: Text); 將檔案緩衝區寫入磁碟 |
35 |
function IOResult: Word; 返回上次檔案 IO 操作的結果 |
36 |
procedure Read(var F: Text; Args: Arguments); 從檔案讀取到變數中 |
37 |
procedure Read(Args: Arguments); 從檔案讀取到變數中 |
38 |
procedure ReadLn(var F: Text; Args: Arguments); 從檔案讀取到變數中並轉到下一行 |
39 |
procedure ReadLn(Args: Arguments); 從檔案讀取到變數中並轉到下一行 |
40 |
procedure Rename(var f: file; const s:); 重新命名磁碟上的檔案 |
41 |
procedure Rename(var f: file; p: PChar); 重新命名磁碟上的檔案 |
42 |
procedure Rename(var f: file; c: Char); 重新命名磁碟上的檔案 |
43 |
procedure Rename(var t: Text; const s); 重新命名磁碟上的檔案 |
44 |
procedure Rename(var t: Text; p: PChar); 重新命名磁碟上的檔案 |
45 |
procedure Rename( var t: Text; c: Char); 重新命名磁碟上的檔案 |
46 |
procedure Reset(var f: file; l: LongInt); 開啟檔案以供讀取 |
47 |
procedure Reset(var f: file); 開啟檔案以供讀取 |
48 |
procedure Reset(var f: TypedFile); 開啟檔案以供讀取 |
49 |
procedure Reset(var t: Text); 開啟檔案以供讀取 |
50 |
procedure Rewrite(var f: file; l: LongInt); 開啟檔案以供寫入 |
51 |
procedure Rewrite(var f: file); 開啟檔案以供寫入 |
52 |
procedure Rewrite(var f: TypedFile); 開啟檔案以供寫入 |
53 |
procedure Rewrite(var t: Text); 開啟檔案以供寫入 |
54 |
procedure Seek(var f: file; Pos: Int64); 設定檔案位置 |
55 |
function SeekEOF(var t: Text):Boolean; 將檔案位置設定為檔案末尾 |
56 |
function SeekEOF: Boolean; 將檔案位置設定為檔案末尾 |
57 |
function SeekEOLn(var t: Text):Boolean; 將檔案位置設定為行尾 |
58 |
function SeekEOLn: Boolean; 將檔案位置設定為行尾 |
59 |
procedure SetTextBuf(var f: Text; var Buf); 設定檔案緩衝區的大小 |
60 |
procedure SetTextBuf(var f: Text; var Buf; Size: SizeInt); 設定檔案緩衝區的大小 |
61 |
procedure Truncate(var F: file); 截斷指定位置的檔案 |
62 |
procedure Write(Args: Arguments); 將變數寫入檔案 |
63 |
procedure Write(var F: Text; Args: Arguments); 將變數寫入檔案 |
64 |
procedure Writeln(Args: Arguments); 將變數寫入檔案並追加換行符 |
65 |
procedure WriteLn(var F: Text; Args: Arguments); 將變數寫入檔案並追加換行符 |