Delphi文件 FileOpen、FileSeek等的用法

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Label1: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Label2: TLabel;
Button2: TButton;
Button3: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;
//定义记录类型
TRecEmp = record
//需要初始化
EmpNo: String[20];
EmpName: String[30];
end;

var
Form1: TForm1;
RecEmp: TRecEmp;
//定义一个记录类型的文件变量
FileText: file of TRecEmp;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
last: LongInt;
begin
RecEmp.EmpNo := trim(Edit1.Text);
RecEmp.EmpName := trim(Edit2.Text);
try
//和某个文件建立链接
AssignFile(FileText,'d:\sql.txt');
//打开一个已经存在的文件
Reset(FileText);
last := FileSize(FileText);
//将指针移向指定记录
Seek(FileText, last);
//写文件
Write(FileText, RecEmp);
Edit1.Text := '';
Edit2.Text := '';
CloseFile(FileText);
except
ReWrite(FileText);
end;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
try
AssignFile(FileText,'d:\sql.txt');
Reset(FileText);
while not Eof(FileText) do
begin
//读文件
Read(FileText, RecEmp);
Memo1.Lines.Add(RecEmp.EmpNo + RecEmp.EmpName);
end;
finally
//关闭文件
CloseFile(FileText);
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PChar;
i: Integer;

begin
try
//打开一个特定的文件并返回文件句柄
iFileHandle := FileOpen('d:\file.txt', fmOpenRead);
//返回文件共多少个字符
iFileLength := FileSeek(iFileHandle,1,0);

//指针指向文件开头
FileSeek(iFileHandle,0,0);
Buffer := PChar(AllocMem(iFileLength + 1));
//读取第三个参数的大小到内存Buffer,返回读取几个字符
iBytesRead := FileRead(iFileHandle, Buffer^, iFileLength+1);
//关闭文件
FileClose(iFileHandle);
with StringGrid1 do
begin
Cols[1].Text := '姓名';
Cols[2].Text := '分数';
//设置字体颜色
Canvas.Font.Color := clGreen;

end;

for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
//Cells[第几列,第几行] 都是从0开始的
StringGrid1.Cells[1,i+1] := Buffer[i];
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
end;
finally
//释放内存
Freemem(Buffer);
end;
end;

end.


//以下是注释
{
function FileOpen(const FileName: string; Mode: LongWord): Integer;
函数变量Mode的取值:

fmCreate If the file exists, open for write access, otherwise, create a new file. Unlike the other constants, which are declared in the SysUtils unit, this constant is declared in tge classes unit.
fmOpenRead Open for read access only.
fmOpenWrite Open for write access only.
fmOpenReadWrite Open for read and write access.
fmShareCompat Compatible with the way FCBs are opened. Do not use this mode in cross-platform applications.
fmShareExclusive Read and write access is denied.

fmShareDenyWrite Write access is denied.
fmShareDenyRead Read access is denied. Do not use this mode in cross-platform applications.
fmShareDenyNone Allows full access for others.
}

{
function FileSeek(Handle, Offset, Origin: Integer): Integer; overload;
变量Origin的取值:

0 The file pointer is positioned Offset bytes from the beginning of the file.
1 The file pointer is positioned Offset bytes from its current position.
2 The file pointer is positioned Offset bytes from the end of the file.
}

{
分配内存
function AllocMem(Size: Cardinal): Pointer;
}

{
FileRead reads Count bytes from the file specified by Handle into the buffer
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值