1. 程式人生 > 實用技巧 >delphi中無型別檔案讀寫

delphi中無型別檔案讀寫

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.

  1. unitUnit1;
  2. interface
  3. uses
  4. Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
  5. Dialogs,StdCtrls;
  6. type
  7. TForm1=class(TForm)
  8. Memo1:TMemo;
  9. Button1:TButton;
  10. Button2:TButton;
  11. Button3:TButton;
  12. Button4:TButton;
  13. Button5:TButton;
  14. Button6:TButton;
  15. Button7:TButton;
  16. procedureButton1Click(Sender:TObject);
  17. procedureButton2Click(Sender:TObject);
  18. procedureButton3Click(Sender:TObject);
  19. procedureButton4Click(Sender:TObject);
  20. procedureButton5Click(Sender:TObject);
  21. procedureButton6Click(Sender:TObject);
  22. procedureButton7Click(Sender:TObject);
  23. private
  24. {Privatedeclarations}
  25. public
  26. {Publicdeclarations}
  27. end;
  28. var
  29. Form1:TForm1;
  30. implementation
  31. {$R*.dfm}
  32. const
  33. FileName='c:\temp\binary.dat';
  34. var
  35. F:file;
  36. //用無型別的方式寫入一個位元組
  37. procedureTForm1.Button1Click(Sender:TObject);
  38. var
  39. b:Byte;
  40. begin
  41. AssignFile(F,FileName);
  42. Rewrite(F,1);//第二個引數表示:把1個位元組當作一個寫入單位
  43. b:=65;
  44. BlockWrite(F,b,1);//寫入;引數2是要寫入的內容;引數3表示寫入1次.
  45. CloseFile(F);
  46. //落實binary.dat的大小:1個位元組;內容:A(可用記事本開啟)
  47. end;
  48. //用無型別的方式讀出一個位元組
  49. procedureTForm1.Button2Click(Sender:TObject);
  50. var
  51. b:Byte;
  52. begin
  53. AssignFile(F,FileName);
  54. Reset(F,1);//把1個位元組當作一個讀取單位
  55. BlockRead(F,b,1);//讀取一次;放入b
  56. ShowMessage(Chr(b));//A
  57. CloseFile(F);
  58. end;
  59. //寫入更多位元組
  60. procedureTForm1.Button3Click(Sender:TObject);
  61. var
  62. buffer:array[0..127]ofByte;
  63. i:Integer;//儲存多個位元組,需要用陣列了
  64. begin
  65. fori:=Low(buffer)toHigh(buffer)do
  66. buffer[i]:=i;//給陣列賦值,注意現在陣列大小是128
  67. AssignFile(F,FileName);
  68. Rewrite(F,32);//規定把32個位元組當作一個讀取單位,注意這個數字和緩衝區大小必須是倍數關係
  69. BlockWrite(F,buffer,4);//需要寫入幾次呢?128/32=4
  70. CloseFile(F);
  71. //寫成的檔案肯定是128位元組大小,但用記事本可能看不了,因為這個二進位制不是文字
  72. end;
  73. //讀出
  74. procedureTForm1.Button4Click(Sender:TObject);
  75. var
  76. buffer:array[0..127]ofByte;
  77. i:Integer;
  78. begin
  79. AssignFile(F,FileName);
  80. Reset(F,4);//把4位元組當作一個讀取單位;為了那個倍數關係,這裡一般是1
  81. BlockRead(F,buffer,32);//當然需要32次才能讀完
  82. CloseFile(F);
  83. //怎麼顯示一下呢?還是用Memo吧;顯示字元?數字?還是十六進位制吧
  84. Memo1.Clear;
  85. fori:=Low(buffer)toHigh(buffer)do
  86. begin
  87. Memo1.Lines.Add(IntToHex(buffer[i],1));
  88. end;
  89. (*顯示結果:0123...7D7E7F*)
  90. end;
  91. //讀寫Char與實際讀寫位元組
  92. procedureTForm1.Button5Click(Sender:TObject);
  93. var
  94. cArr1,cArr2:array[0..4]ofChar;
  95. i:Integer;
  96. num:Integer;//記錄實際讀寫的位元組數
  97. begin
  98. fori:=Low(cArr1)toHigh(cArr1)do
  99. cArr1[i]:=Chr(65+i);//填充ABCDE
  100. AssignFile(F,FileName);
  101. Rewrite(F,1);//建立
  102. BlockWrite(F,cArr1,Length(cArr1),num);//引數4:num是實際寫入的位元組數
  103. ShowMessage(IntToStr(num));//5
  104. Reset(F,1);//重新開啟
  105. BlockRead(F,cArr2,Length(cArr2),num);//引數5:num是實際讀出的位元組數
  106. ShowMessage(IntToStr(num));//5
  107. ShowMessage(cArr2);//ABCDE
  108. CloseFile(F);
  109. end;
  110. //寫入長字串,讀出其十六進位制碼
  111. procedureTForm1.Button6Click(Sender:TObject);
  112. var
  113. p:PChar;
  114. b:Byte;
  115. begin
  116. p:='萬一的Delphi部落格';
  117. AssignFile(F,FileName);//關聯檔名
  118. Rewrite(F,1);//建立並開啟
  119. BlockWrite(F,p,Length(p));//寫入
  120. FileMode:=fmOpenRead;//這時可以設檔案為只讀
  121. Reset(F,1);//為讀取開啟
  122. Memo1.Clear;
  123. whilenotEof(F)do
  124. begin
  125. BlockRead(F,b,1);
  126. Memo1.Text:=Memo1.Text+IntToHex(b,2)+'';
  127. end;
  128. //顯示結果:70F0450008F71200A6FB4300A01AE500FFC5
  129. CloseFile(F);
  130. end;
  131. //複製個檔案
  132. procedureTForm1.Button7Click(Sender:TObject);
  133. var
  134. FromF,ToF:file;
  135. NumRead,NumWritten:Integer;
  136. Buffer:array[1..2048]ofByte;
  137. begin
  138. AssignFile(FromF,'c:\temp\test1.dat');//假定有這個檔案
  139. Reset(FromF,1);
  140. AssignFile(ToF,'c:\temp\test2.dat');
  141. Rewrite(ToF,1);
  142. repeat
  143. BlockRead(FromF,Buffer,SizeOf(Buffer),NumRead);
  144. BlockWrite(ToF,Buffer,NumRead,NumWritten);
  145. until(NumRead=0)or(NumWritten<>NumRead);
  146. {NumRead=0表示讀完了;NumWritten<>NumRead表示磁碟空間不夠了}
  147. CloseFile(FromF);
  148. CloseFile(ToF);
  149. end;
  150. end.