delphi TextFile读取文本文件  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi TextFile读取文本文件


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

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.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.039430141448975 seconds