Unit1.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:1k
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TForm1 = class(TForm)
- btnRead: TButton;
- btnWrite: TButton;
- procedure btnReadClick(Sender: TObject);
- procedure btnWriteClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.btnReadClick(Sender: TObject);
- var
- UnTypedFile: file;
- Buffer: array[0..127] of Byte;
- NumRead: Integer;
- begin
- AssignFile(UnTypedFile, 'MyFile.dat');
- Reset(UnTypedFile);
- try
- {打开MyFile.dat,将128个字节的数据块移到缓冲区Buffer中}
- BlockRead(UnTypedFile, Buffer, 1, NumRead);
- finally
- CloseFile(UnTypedFile);
- end;
- end;
- procedure TForm1.btnWriteClick(Sender: TObject);
- var
- UnTypedFile: file;
- Buffer: array[0..127] of Byte;
- NumWrite: integer;
- begin
- AssignFile(UnTypedFile, 'MyFile.dat');
- //如果文件不存在,就新建一个文件,否则,打开该文件并允许读写操作
- if FileExists('MyFile.dat') then
- Reset(UnTypedFile)
- else
- ReWrite(UnTypedFile);
- try
- //将文件指针置于文件末尾
- Seek(UnTypedFile, FileSize(untypedFile));
- FillChar(Buffer, Sizeof(Buffer), 'Y');
- BlockWrite(UnTypedFile, Buffer, 1, NumWrite);
- finally
- CloseFile(UnTypedFile);
- end;
- end;
- end.