zoukankan      html  css  js  c++  java
  • 无类型文件读写

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Button5: TButton;
        Button6: TButton;
        Button7: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button5Click(Sender: TObject);
        procedure Button6Click(Sender: TObject);
        procedure Button7Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    const
      FileName = 'c:\temp\binary.dat';
    var
      F: file;
    
    //用无类型的方式写入一个字节
    procedure TForm1.Button1Click(Sender: TObject);
    var
      b: Byte;
    begin
      AssignFile(F,FileName);
      Rewrite(F,1); //第二个参数表示: 把 1 个字节当作一个写入单位
    
      b := 65;
      BlockWrite(F,b,1); //写入; 参数2是要写入的内容; 参数3表示写入 1 次.
    
      CloseFile(F);
    //落实 binary.dat 的大小: 1个字节; 内容: A (可用记事本打开)
    end;
    
    
    //用无类型的方式读出一个字节
    procedure TForm1.Button2Click(Sender: TObject);
    var
      b: Byte;
    begin
      AssignFile(F,FileName);
      Reset(F,1); //把 1 个字节当作一个读取单位
    
      BlockRead(F,b,1); //读取一次; 放入 b
      ShowMessage(Chr(b)); //A
    
      CloseFile(F);
    end;
    
    
    //写入更多字节
    procedure TForm1.Button3Click(Sender: TObject);
    var
      buffer: array[0..127] of Byte;
      i: Integer; //储存多个字节, 需要用数组了
    begin
      for i := Low(buffer) to High(buffer) do
        buffer[i] := i; //给数组赋值, 注意现在数组大小是 128
    
      AssignFile(F,FileName);
      Rewrite(F,32); //规定把 32 个字节当作一个读取单位, 注意这个数字和缓冲区大小必须是倍数关系
    
      BlockWrite(F,buffer,4); //需要写入几次呢? 128/32=4
    
      CloseFile(F);
    //写成的文件肯定是128字节大小, 但用记事本可能看不了, 因为这个二进制不是文本
    end;
    
    
    //读出
    procedure TForm1.Button4Click(Sender: TObject);
    var
      buffer: array[0..127] of Byte;
      i: Integer;
    begin
      AssignFile(F,FileName);
      Reset(F,4); //把 4 字节当作一个读取单位; 为了那个倍数关系, 这里一般是 1
    
      BlockRead(F,buffer,32); //当然需要 32 次才能读完
    
      CloseFile(F);
    
      //怎么显示一下呢? 还是用 Memo 吧; 显示字符? 数字? 还是十六进制吧
      Memo1.Clear;
      for i := Low(buffer) to High(buffer) do
      begin
        Memo1.Lines.Add(IntToHex(buffer[i],1));
      end;
    (*显示结果: 0 1 2 3 ... 7D 7E 7F *)
    
    end;
    
    
    //读写 Char 与实际读写字节
    procedure TForm1.Button5Click(Sender: TObject);
    var
      cArr1,cArr2: array[0..4] of Char;
      i: Integer;
      num: Integer; //记录实际读写的字节数
    begin
      for i := Low(cArr1) to High(cArr1) do
        cArr1[i] := Chr(65+i); //填充 A B C D E
    
      AssignFile(F,FileName);
      Rewrite(F,1); //建立
    
      BlockWrite(F,cArr1,Length(cArr1),num); //参数4: num 是实际写入的字节数
      ShowMessage(IntToStr(num)); //5
    
      Reset(F,1); //重新打开
    
      BlockRead(F,cArr2,Length(cArr2),num); //参数5: num 是实际读出的字节数
      ShowMessage(IntToStr(num)); //5
    
      ShowMessage(cArr2); //ABCDE
    
      CloseFile(F);
    end;
    
    
    //写入长字符串, 读出其十六进制码
    procedure TForm1.Button6Click(Sender: TObject);
    var
      p: PChar;
      b: Byte;
    begin
      p := '万一的 Delphi 博客';
    
      AssignFile(F,FileName);     //关联文件名
      Rewrite(F,1);               //创建并打开
      BlockWrite(F,p,Length(p));  //写入
    
    
      FileMode := fmOpenRead;     //这时可以设文件为只读
      Reset(F,1);                 //为读取打开
    
      Memo1.Clear;
      while not Eof(F) do
      begin
        BlockRead(F,b,1);
        Memo1.Text := Memo1.Text + IntToHex(b,2) + ' ';
      end;
      //显示结果: 70 F0 45 00 08 F7 12 00 A6 FB 43 00 A0 1A E5 00 FF C5
    
      CloseFile(F);
    end;
    
    
    //复制个文件
    procedure TForm1.Button7Click(Sender: TObject);
    var
      FromF,ToF: file;
      NumRead,NumWritten: Integer;
      Buffer: array[1..2048] of Byte;
    begin
      AssignFile(FromF, 'c:\temp\test1.dat'); //假定有这个文件
      Reset(FromF, 1);
      AssignFile(ToF, 'c:\temp\test2.dat');
      Rewrite(ToF, 1);
    
      repeat
        BlockRead(FromF, Buffer, SizeOf(Buffer), NumRead);
        BlockWrite(ToF, Buffer, NumRead, NumWritten);
      until (NumRead = 0) or (NumWritten <> NumRead);
      { NumRead=0 表示读完了; NumWritten <> NumRead 表示磁盘空间不够了 }
    
      CloseFile(FromF);
      CloseFile(ToF);
    end;
    
    end.
    
  • 相关阅读:
    [转]人生哲理小故事
    取PE文件OriginalFilename解析VERSION资源
    [转]COM对象创建外部机制
    读书的几个步骤
    zoj 2412 Farm Irrigation
    HDU 1575 Tr A
    toj 2843 Diamonds
    HDU 1856 More is better
    toj 2841 Bitwise Reverse
    hdu 1213 How Many Tables
  • 原文地址:https://www.cnblogs.com/del/p/1007420.html
Copyright © 2011-2022 走看看