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
}
//www.delphitop.com 搜集整理

相关阅读 >>

Delphi 读取剪粘板内的html格式数据

Delphi 操作“任务栏”

Delphi checklistbox用法

汇编基础知识

Delphi 常用4种对话框

Delphi根据字符分割字串成数组

Delphi idmessage1 idsmtp1 发送邮件支持https

Delphi 校验文件大小

Delphi 查找指定目录,指定扩展名的所有文件名

Delphi listview 导出excel txt vcf 单元

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...