delphi中無型別檔案讀寫
阿新 • • 發佈:2020-08-01
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.
- unitUnit1;
- 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;
- procedureButton1Click(Sender:TObject);
- procedureButton2Click(Sender:TObject);
- procedureButton3Click(Sender:TObject);
- procedureButton4Click(Sender:TObject);
- procedureButton5Click(Sender:TObject);
- procedureButton6Click(Sender:TObject);
- procedureButton7Click(Sender:TObject);
- private
- {Privatedeclarations}
- public
- {Publicdeclarations}
- end;
- var
- Form1:TForm1;
- implementation
- {$R*.dfm}
- const
- FileName='c:\temp\binary.dat';
- var
- F:file;
- //用無型別的方式寫入一個位元組
- procedureTForm1.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;
- //用無型別的方式讀出一個位元組
- procedureTForm1.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;
- //寫入更多位元組
- procedureTForm1.Button3Click(Sender:TObject);
- var
- buffer:array[0..127]ofByte;
- i:Integer;//儲存多個位元組,需要用陣列了
- begin
- fori:=Low(buffer)toHigh(buffer)do
- buffer[i]:=i;//給陣列賦值,注意現在陣列大小是128
- AssignFile(F,FileName);
- Rewrite(F,32);//規定把32個位元組當作一個讀取單位,注意這個數字和緩衝區大小必須是倍數關係
- BlockWrite(F,buffer,4);//需要寫入幾次呢?128/32=4
- CloseFile(F);
- //寫成的檔案肯定是128位元組大小,但用記事本可能看不了,因為這個二進位制不是文字
- end;
- //讀出
- procedureTForm1.Button4Click(Sender:TObject);
- var
- buffer:array[0..127]ofByte;
- i:Integer;
- begin
- AssignFile(F,FileName);
- Reset(F,4);//把4位元組當作一個讀取單位;為了那個倍數關係,這裡一般是1
- BlockRead(F,buffer,32);//當然需要32次才能讀完
- CloseFile(F);
- //怎麼顯示一下呢?還是用Memo吧;顯示字元?數字?還是十六進位制吧
- Memo1.Clear;
- fori:=Low(buffer)toHigh(buffer)do
- begin
- Memo1.Lines.Add(IntToHex(buffer[i],1));
- end;
- (*顯示結果:0123...7D7E7F*)
- end;
- //讀寫Char與實際讀寫位元組
- procedureTForm1.Button5Click(Sender:TObject);
- var
- cArr1,cArr2:array[0..4]ofChar;
- i:Integer;
- num:Integer;//記錄實際讀寫的位元組數
- begin
- fori:=Low(cArr1)toHigh(cArr1)do
- cArr1[i]:=Chr(65+i);//填充ABCDE
- 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;
- //寫入長字串,讀出其十六進位制碼
- procedureTForm1.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;
- whilenotEof(F)do
- begin
- BlockRead(F,b,1);
- Memo1.Text:=Memo1.Text+IntToHex(b,2)+'';
- end;
- //顯示結果:70F0450008F71200A6FB4300A01AE500FFC5
- CloseFile(F);
- end;
- //複製個檔案
- procedureTForm1.Button7Click(Sender:TObject);
- var
- FromF,ToF:file;
- NumRead,NumWritten:Integer;
- Buffer:array[1..2048]ofByte;
- 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.