RSS订阅 | 匿名投稿
您的位置:网站首页 > 文件 > 正文

Delphi TextFile读取文本文件

作者:admin 来源:未知 日期:2010/5/9 14:31:04 人气: 标签:

源代码:
------------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  p rivate
    { P rivate declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
//{$APPTYPE CONSOLE}

procedure TForm1.Button1Click(Sender: TObject);
var
  txt:TextFile;
  s:string;
  path:string;
begin
  path:=ExtractFilePath(Application.ExeName);
  AssignFile(txt,path+'\test.txt');
  //Writeln(path+'\test.txt');
  Reset(txt);   //读打开文件,文件指针移到首
  Memo1.Clear;
  while not Eof(txt) do
  begin
    Readln(txt,s);
    Memo1.Lines.Add(s);
  end;
  CloseFile(txt);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  txt:TextFile;
  s:string;
  path:string;
begin
  path:=ExtractFilePath(Application.ExeName);
  Memo1.Clear;
  AssignFile(txt,path+'\test.txt');
  Append(txt);  //写打开文件,指针到尾
  Writeln(txt,'这是猪悟能写入的文本');  //写入一行带行结束标志
  CloseFile(txt);

end;

procedure TForm1.Button3Click(Sender: TObject);
var
  txt:TextFile;
  s,path:string;
begin
  path:=ExtractFilePath(Application.ExeName);
  Memo1.Clear;
  AssignFile(txt,path+'\test.txt');
  Reset(txt);

  Readln(txt,s);
  //Memo1.Lines.Add(IntToStr(FileSize(txt)));
  Memo1.Lines.Add(s);
  CloseFile(txt);
end;

end.


读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址: