zoukankan      html  css  js  c++  java
  • 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.
    书搞进脑袋 创新 创造; 积极
  • 相关阅读:
    Fileupload文件上传下载
    携程运维自动化平台,上万服务器变更也可以很轻松
    判断某个日期是不是该月的第一天或最后一天
    js format 设置日期格式 将Fri Dec 12 2014 08:00:00 GMT+0800改为2014-12-12 8:00:00
    js13位时间戳转换,10位时间戳转换
    java io包File类
    为什么前端实现的页面跟设计师的设计稿的差别那么大?
    为什么前端实现的页面跟设计师的设计稿的差别那么大?
    为什么前端实现的页面跟设计师的设计稿的差别那么大?
    为什么前端实现的页面跟设计师的设计稿的差别那么大?
  • 原文地址:https://www.cnblogs.com/tobetterlife/p/12173037.html
Copyright © 2011-2022 走看看