Delphi文字檔案讀寫

2021-06-16 08:12:11 字數 4465 閱讀 5887

delphi 支援三種檔案型別: 文字檔案、記錄檔案、無型別檔案。

文字檔案是以行為單位進行讀、寫的。由於每一行的長度不一定相同,不能計算出給定行在檔案中的確切位置,因而只能順序地讀寫。

文字檔案只能單獨為讀或寫而開啟,在乙個開啟的文字檔案上同時進行讀、寫操作是不允許的。

1.檔案變數與檔名關聯:

assignfile(vartxt, filename);

filename 如果省略路徑將預設當前目錄。

2.初始化讀寫有三種方式:

(1) reset: 唯讀開啟, 指標移到檔案頭;

(2) rewrite: 建立新檔案並開啟, 只寫;

最後用 closefile 關閉檔案。

為保持相容,delphi 也允許使用者用 assign 建立關聯, 用 close 關閉檔案。

//讀寫示例:

unit unit1;

inte***ce

uses windows, messages, sysutils, variants, classes, graphics, controls,forms,  dialogs, stdctrls;

type tform1 = class(tform)

memo1: tmemo;

button1: tbutton;

button2: tbutton;

button3: tbutton;

button4: tbutton;

button5: tbutton;

button6: tbutton;

button7: tbutton;

procedure button1click(sender: tobject);

procedure button2click(sender: tobject);

procedure button3click(sender: tobject);

procedure button4click(sender: tobject);

procedure button5click(sender: tobject);

procedure button6click(sender: tobject);

procedure button7click(sender: tobject);

private   

public   

end;

var form1: tform1;

implementation

varf:text;  //textfile 和 text 是一樣的

filename: string = 'c:\temp\test.txt';

//寫檔案

procedure tform1.button1click(sender:tobject);

begin

assignfile(f,filename);

rewrite(f);  //會覆蓋已存在的檔案

writeln(f, '第一行');

writeln(f, '第二行');

closefile(f);

end;

//讀檔案(先執行寫檔案)

procedure tform1.button2click(sender:tobject);

vars:string;

begin

assignfile(f,filename);

reset(f);       //唯讀開啟

readln(f, s);   //讀取

showmessage(s); //顯示: 第一行

readln(f, s);   //繼續讀取

showmessage(s); //顯示: 第二行

closefile(f);

end;

//追加

procedure tform1.button3click(sender:tobject);

begin

assignfile(f,filename);

writeln(f, '第三行');

writeln(f, '第四行');

closefile(f);

end;

//讀取全部(需要memo幫忙顯示)

procedure tform1.button4click(sender:tobject);

vars:string;

begin

assignfile(f,filename);

reset(f);  //唯讀開啟

memo1.clear;

while not eof(f) do

begin

readln(f, s);

memo1.lines.add(s);

end;

closefile(f);

end;

//分列寫入

procedure tform1.button5click(sender:tobject);  //這個過程級函式的功能是: 用空格把 s 湊夠 n 的長度 

function addspace(s: string; n: word):string;

begin

while length(s) < n do

begin

s := s + ' ';

end;

result := s;

end;

varname: string[8];

address: string[16];

begin

assignfile(f, filename);

rewrite(f);

name := '張三';

name := addspace(name,8);

address := '山東泰安';

address := addspace(address,16);

writeln(f, name, address);

name := '李四兒';

name := addspace(name,8);

address := '貴州省安順黃果樹';

address := addspace(address,16);

writeln(f, name, address);

name := '王二麻子';

name := addspace(name,8);

address := addspace(address,16);

writeln(f, name, address);

closefile(f);

end;

//分列讀取

procedure tform1.button6click(sender:tobject);

var name: string[8];

address: string[16];

begin

assignfile(f, filename);

reset(f);

memo1.clear;

while not eof(f) do

begin

readln(f, name);

memo1.lines.add(name);

end;

closefile(f);

reset(f);

while not eof(f) do

begin

readln(f, name, address);

memo1.lines.add(address);

end;

closefile(f);

end;

//不同資料型別的寫入和讀取

procedure tform1.button7click(sender:tobject);

var name: string[6];

age: word;

birthday: tdate;

begin

assignfile(f,filename);

rewrite(f);

name := '菜花  ';  //加兩個空格湊夠6個字元

age:= 18;

birthday := strtodate(datetostr(now-18*365));  //假如她今天過生日

writeln(f,name,age,birthday);

closefile(f);

reset(f);

readln(f,name,age,birthday);

memo1.clear;

memo1.lines.add(name);

memo1.lines.add(inttostr(age));

memo1.lines.add(datetostr(birthday));

closefile(f);

//其實這樣的東西應該用型別檔案操作更合適, 但如果有這樣的文字檔案讓你讀取呢?

end;

end. 

Delphi文字檔案讀寫

delphi文字檔案讀寫 2008 10 31 19 07 22 delphi 支援三種檔案型別 文字檔案 記錄檔案 無型別檔案。文字檔案是以行為單位進行讀 寫的。由於每一行的長度不一定相同,不能計算出給定行在檔案中的確切位置,因而只能順序地讀寫。文字檔案只能單獨為讀或寫而開啟,在乙個開啟的文字檔案...

讀寫文字檔案

讀文字 function readtext filename string string vars string alltext string f textfile begin assignfile f,filename 將c myfile.txt檔案與f變數建立連線,後面可以使用f變數對檔案進行操...

python 讀寫文字檔案

本人最近新學python 用到文字檔案的讀取,經過一番研究,從網上查詢資料,經過測試,總結了一下讀取文字檔案的方法.a f open filename r content f.read decode utf 8 b f codecs.open encoding utf 8 content f.rea...