虽然标示符已经写得很清楚了,但是我还是对其中的确切含义不理解,
我打开文件用的是fmOpenReadAndWrite,但是我想问的是,写文件的时候,为什么还需要read呢?
我打开文件用的是fmOpenReadAndWrite,但是我想问的是,写文件的时候,为什么还需要read呢?
解决方案 »
- 为什么昨天程序好好的今天就不行了???
- 各位请教一下怎么把TXT数据导入ACCESS
- 为什么Form1调用form2时会出错呢。不同的调用方法,结果却是完全不一样。。。。高手指教
- 在线请教(100分相赠),ActiveForm怎样将文件写入本地硬盘!!急!!!
- 有没有广东清远的DELPHI高手???
- 动态设置ADOConnectiong的ConnectionString属性问题
- Delphi高手们,错误“MSXML is not installed!”是什么原因?
- 关于一个字符串问题//在线
- 如何在程序中显示HTML文件?用WebBrowse控件?
- 求助:在已有数据集的前提下,怎么才能不多次访问数据库达到模糊查询的目的?
- OpenGL和DirectX哪个更容易上手?
- CXGRID 添加计算字段
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function FileOpen(const FileName: string; Mode:
Integer):Integer;
**** 开档失败传回-1.
说明 以下有关档案读取都属低阶,如Dos Int 21h中有关档案的部
分.
fmOpenRead = $0000;
fmOpenWrite = $0001;
fmOpenReadWrite = $0002;
fmShareCompat = $0000;
fmShareExclusive = $0010;
fmShareDenyWrite = $0020;
fmShareDenyRead = $0030;
fmShareDenyNone = $0040; 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.
fmShareExclusive Read and write access is denied.
fmShareDenyWrite Write access is denied.
fmShareDenyRead Read access is denied.
fmShareDenyNone Allows full access for others.
范例
procedure OpenForShare(const FileName: String);
var
FileHandle : Integer;
begin
FileHandle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
if FileHandle > 0 then
{valid file handle}
else
{Open error: FileHandle = negative DOS error code}
end;
范例
procedure TForm1.Button1Click(Sender: TObject);
var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PChar;
i: Integer
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer := PChar(AllocMem(iFileLength + 1));
iBytesRead = FileRead(iFileHandle, Buffer, iFileLength);
Fil
eClose(iFileHandle);
for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffer[i];
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
end;
finally
FreeMem(Buffer);
end;
end;
end;
##FileOpen, FileSeek, FileRead Example
-----------------------------------------------------------------------------
FileCreate 建档
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function FileCreate(const FileName: string): Integer;范例
procedure TForm1.Button1Click(Sender: TObject);
var
BackupName: string;
FileHandle: Integer;
StringLen: Integer;
X: Integer;
Y: Integer;
begin
if SaveDialog1.Execute then
begin
if FileExists(SaveDialog1.FileName) then
begin
BackupName := ExtractFileName(SaveDialog1.FileName);
BackupName := ChangeFileExt(BackupName, '.BAK');
if not RenameFile(SaveDialog1.FileName, BackupName) then raise Exception.Create('Unable to create backup file.');
end;
FileHandle := FileCreate(SaveDialog1.FileName);
{ Write out the number of rows and columns in the grid. }
FileWrite(FileHandle,
StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
for X := 0 to StringGrid1.ColCount ? 1 do
begin for Y := 0 to StringGrid1.RowCount ? 1 do
begin
{ Write out the length of each string, followed by the string itself. }
StringLen := Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle, StringLen, SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;end;
##FileExists, RenameFile, FileCreate, FileWrite, FileClose, ExtractFileName Example
-----------------------------------------------------------------------------
FileClose 关档
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 procedure FileClose(Handle: Integer);
范例
procedure TForm1.Button1Click(Sender: TObject);
var
BackupName: string;
FileHandle: Integer;
StringLen: Integer;
X: Integer;
Y: Integer;
begin
if SaveDialog1.Execute then
begin
if FileExists(SaveDialog1.FileName) then
begin
BackupName := ExtractFileName(SaveDialog1.FileName);
BackupName := ChangeFileExt(BackupName, '.BAK');
if not RenameFile(SaveDialog1.FileName, BackupName) then
raise Exception.Create('Unable to create backup file.');
end;
FileHandl
e := FileCreate(SaveDialog1.FileName);
{ Write out the number of rows and columns in the grid. }
FileWrite(FileHandle,
StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
for X := 0 to StringGrid1.ColCount ? 1 do
begin
for Y := 0 to StringGrid1.RowCount ? 1 do
begin
{ Write out the length of each string, followed by the string itself. }
StringLen := Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle, StringLen, SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;
end;
##FileExists, RenameFile, FileCreate, FileWrite, FileClose, ExtractFileName Example============================================
**** 它是以Handle为叁数.
============================================
FileRead 读取档案
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function FileRead(Handle: Integer; var Buffer; Count: Integer):Integer;
范例
procedure TForm1.Button1Click(Sender: TObject);var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PChar;
i: Integer
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer := PChar(AllocMem(iFileLength + 1));
iBytesRead = FileRead(iFileHandle, Buffer, iFileLength);
FileClose(iFileHandle);
for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffer[i];
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
end;
finally
FreeMem(Buffer);
end;
end;
end;
##FileOpen, FileSeek, FileRead Example
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function FileWrite(Handle: Integer; const Buffer; Count: Integer): Integer;
范例
procedure TForm1.Button1Click(Sender: TObject);
var
BackupName: string;
FileHandle: Integer;
StringLen: Integer;
X: Integer;
Y: Integer;
begin
if SaveDialog1.Execute then
begin
if FileExists(SaveDialog1.FileName) then
begin
BackupName := ExtractFileName(SaveDialog1.FileName);
BackupName := ChangeFileExt(BackupName, '.BAK');
if not RenameFile(SaveDialog1.FileName, BackupName) then
raise Exception.Create('Unable to create backup file.');
end;
FileHandle := FileCreate(SaveDialog1.FileName);
{ Write out the number of rows and columns in the grid. }
FileWrite(FileHandle,
StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
for X := 0 to StringGrid1.ColCount do
begin
for Y := 0 to StringGrid1.RowCount do
begin
{ Write out the length of each string, followed by the string itself. }
StringLen := Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle, StringLen, SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);//?????????/
end;
end;
FileClose(FileHandle);
end;
end;
##FileExists, RenameFile, FileCreate, FileWrite, FileClose, ExtractFileName Example
-----------------------------------------------------------------------------
FileSeek 移动档案指标位置
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function FileSeek(Handle, Offset, Origin: Integer): Integer;
说明 Origin=0读/写指标由档案开头算起.
Origin=1读/写指标由目前位置算起.
Origin=2读/写指标移动到档案结束处.
**** 功能与Dos Int 21h 插断 42h 的功能相同.
失败传回-1.
范例 procedure TForm1.Button1Click(Sender: TObject);
var
FileHandle : Integer;
FileName : String;
Buffer : PChar;
S : String;
ReadBytes : Integer;
begin
FileName:='c:\delphi_test\abc.ttt';
S:='1234567890';
if FileExists(FileName) then
FileHandle := FileOpen(FileName, fmOpenReadWrite)
else
FileHandle := FileCreate(FileName);
if FileHandle < 0 then
Begin
MessageDlg('开档失败', mtInformation, [mbOk], 0);
Exit;
End; GetMem(Buffer, 100);
try
StrPCopy(Buffer, S);
FileWrite(FileHandle,Buffer^,10);
FileSeek(FileHandle,4,0);
ReadBytes:=FileRead(FileHandle, Buffer^, 100);
Buffer[ReadBytes]:=#0;
Label1.Caption:=IntToStr(ReadBytes)+' '+
StrPas(Buffer);
finally
FreeMem(Buffer);
end; FileClose(FileHandle);
end;结果 存档後abc.ttt共有1234567890等十个Bytes.
从第五位元开始读取,共读取六个位元.
567890
(位移是从0开始算起)procedure TForm1.Button1Click(Sender: TObject);var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffer: PChar;
i: Integer
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffer := PChar(AllocMem(iFileLength + 1));
iBytesRead = FileRead(iFileHandle, Buffer, iFileLength);
FileClose(iFileHandle);
for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffer[i];
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i]));
end;
finally
FreeMem(Buffer);
end;
end;
end;
##FileOpen, FileSeek, FileRead Example
-----------------------------------------------------------------------------
FileGetAttr 档案属性
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function FileGetAttr(const FileName: string): Integer;
说明 faReadOnly = $00000001;
faHidden = $00000002;
faSysFile = $00000004;
faVolumeID = $00000008;
faDirectory = $00000010;
faArchive = $00000020;
faAnyFile = $0000003F;
范例 procedure TForm1.Button1Click(Sender: TObject);
var
S: String;
begin
S:=IntToStr(FileGetAttr('c:\delphi_d\delphi_help1.txt'));
Label1.Caption := S;
end;
-----------------------------------------------------------------------------
FileSetAttr 设定档案属性
-----------------------------------------------------------------------------
Unit SysUtils
函数原型 function FileSetAttr(const FileName: string; Attr: Integer):
Integer;
说明 设定成功传回0
-----------------------------------------------------------------------------
FindClose 结束FindFirst/FindNext
-----------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);var
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then FileAttrs := FileAttrs + faAnyFile; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then begin
with StringGrid1 do
begin
if (sr.Attr and FileAttrs) = sr.Attr then
begin
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
while FindNext(sr) = 0 do
begin
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount + 1;
Cells[1, RowCount-1] := sr.Name; Cells[2, RowCount-1] := IntToStr(sr.Size);
end;
end;
FindClose(sr);
end;
end;
end;
##FindFirst, FindNext, FindClose Example
fmopenreadandwrite是说当你打开这个文件时允许进行读写操作,
fmopenwrite是说,只写方式打开,你打开后不予许其他程序再读取该文件
fmopenread是只读模式,只允许读取,不允许修改
我打开文件的时候,用的标志如上。但是有时候还是会出现:“ 另一个程序正在使用此文件,进程无法访问”。写文件我是专门放在一个类里面进行管理的,不应该出现文件访问冲突才对啊。
fmOpenWrite :以只写方式打开指定文件
fmOpenReadWrite:以写写方式打开指定文件
共享模式: fmShareExclusive:不允许别的程序以任何方式打开该文件
fmShareDenyWrite:不允许别的程序以写方式打开该文件
fmShareDenyRead :不允许别的程序以读方式打开该文件
fmShareDenyNone :别的程序可以以任何方式打开该文件