delphi 文本 记录 流式 读写文件

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    edt1: TEdit;
    edt2: TEdit;
    btn2: TButton;
    btn3: TButton;
    btn4: TButton;
    btn5: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


//文本文件 写和 读
procedure TForm1.btn1Click(Sender: TObject);
var
  f:TextFile;
  s:string;
begin
  AssignFile(f,a.txt);
  try
    //if FileExists(‘a.txt‘) then
//      Append(f)
//    else
      Rewrite(f);

    Writeln(f,edt1.Text,---,edt2.Text);

    ////////////
    Reset(f);
    Readln(f,s);
    edt2.Text:= s;
  finally
    CloseFile(f);
  end;
end;

//记录类型文件 写
procedure TForm1.btn2Click(Sender: TObject);
type
 student=record
  sNo:string[10];
  sName:string[10];
 end;
var
  f:File of student;
  stu:student;
  isize:Integer;
begin
  AssignFile(f,a.dat);
  try
    if not FileExists(a.dat) then
      Rewrite(f)
    else
      Reset(f);
    isize:= FileSize(f);
    Seek(f,isize);
    stu.sNo:= edt1.Text;
    stu.sName:= edt2.Text;
    Write(f,stu);
  finally
    CloseFile(f);
  end;
end;

//记录类型文件 读
procedure TForm1.btn3Click(Sender: TObject);
type
 student=record
  sNo:string[10];
  sName:string[10];
 end;
var
  f:File of student;
  stu:student;
  isize:Integer;
begin
  AssignFile(f,a.dat);
  try
    if not FileExists(a.dat) then
      Rewrite(f)
    else
      Reset(f);
    Seek(f,1);//读取 第二条记录,直接用数字表示
    read(f,stu);
    edt1.Text:=stu.sNo;
    edt2.Text:=stu.sName;
  finally
    CloseFile(f);
  end;
end;

//流写
procedure TForm1.btn4Click(Sender: TObject);
type
  TBuffer = array [0..160]of char;
var
   FileStream: TFileStream;
   tempBuffer:TBuffer;
begin
   StrPcopy(tempBuffer,edt1.Text);
   Filestream:=TFileStream.Create(a.file,fmCreate);
   FileStream.Position:=0;
   FileStream.Write(tempBuffer,length(tempBuffer));
   FreeAndNil(FileStream);
end;

//流读
procedure TForm1.btn5Click(Sender: TObject);
type
  TBuffer = array [0..160]of char;
var
  FileStream: TFileStream;
  ABuffer: TBuffer;
begin
   Filestream:=TFileStream.Create(a.file,fmOpenRead);
   Filestream.Position:=0;
   FileStream.Read(ABuffer,sizeof(ABuffer));
   edt2.Text:= abuffer;
   FreeAndNil(FileStream);
end;


end.

delphi 文本 记录 流式 读写文件

上一篇:.Net WebAPI+Jquery Ajax 请求参数


下一篇:vscode 切换分支时报错:The following untracked working tree files would be overwritten .....