新增4個TButton、1個TMemo、2個TEdit
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
F : textfile;
begin
AssignFile(F,'data.txt');
ReWrite(F);
//寫入數據
WriteLn(F, Edit1.Text);
WriteLn(F, Edit2.Text);
CloseFile(F);
Showmessage('寫入成功');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
F : textfile;
s1,s2 : string;
begin
AssignFile(F,'data.txt');
Reset(F);
//讀取數據
ReadLn(F, s1); //不能直接寫Edit1.Text
ReadLn(F, s2); //不能直接寫Edit2.Text
Edit1.Text := s1;
Edit2.Text := s2;
CloseFile(F);
Showmessage('讀取成功');
end;
procedure TForm1.Button3Click(Sender: TObject);
var
n : array[1..100] of integer;
i : integer;
F : textfile;
begin
AssignFile(F,'number.txt');
Rewrite(F);
for i := 1 to 100 do
begin
n[i] := i;
Writeln(F,n[i]);
end;
CloseFile(F);
Showmessage('寫入成功');
end;
procedure TForm1.Button4Click(Sender: TObject);
var
n : array[1..100] of integer;
i : integer;
F : textfile;
begin
AssignFile(F,'number.txt');
Reset(F);
i := 0;
while not(EOF(F)) do begin
i := i+1;
Readln(F, n[i]);
Memo1.Lines.add(inttostr(n[i]));
end;
closeFile(F);
Showmessage('讀取成功');
end;
end.
沒有留言:
張貼留言