您可以切换到使用异常(使用{$I+}
),然后使用try..except
. (它通常是默认设置,除非您I/O Checking
在“项目选项”对话框(Project->Options->Delphi Compiler->Compiling->Runtime Errors->I/O checking
从主菜单中)中取消选中。
如果未选中该框,它会设置选项{$I-}
,它使用IOResult
。
如果您想继续使用IOResult
,您需要在使用文件功能后进行检查。检查它会自动将 InOutRes 变量重置为0
,清除先前的错误值。
AssignFile (fl, fn);
if not FileExists (fn) then
begin
ReWrite (fl);
if IOResult <> 0 then
// You've had an error.
CloseFile (fl); // Now an empty file exists
end; // if
IOResult
可以在System
单位找到。
You really should be moving away from the old style IO routines, BTW. They're ancient, and don't properly work with Unicode data. You can accomplish the same thing using a TFileStream
, which would give you proper exception handling and support for Unicode. Here's a quick console app sample (tested with XP3 on Win 7):
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, Classes, Windows;
type
TMyRec = record
anInt: Integer;
aBool: Boolean;
aByte: Byte;
end;
var
FS: TFileStream;
MyRec: TMyRec;
const
TheFile = 'C:\TempFiles\test.dat';
begin
MyRec.anInt := 12345;
MyRec.aBool := True;
MyRec.aByte := 128;
FS := TFileStream.Create(TheFile, fmCreate or fmOpenReadWrite);
try
FS.Write(MyRec, SizeOf(TMyRec));
// Clear the content and confirm it's been cleared
FillChar(MyRec, SizeOf(TMyRec), 0);
WriteLn('anInt: ', MyRec.anInt, ' aBool: ', MyRec.aBool, ' aByte: ', MyRec.aByte);
FS.Position := 0;
FS.Read(MyRec, SizeOf(TMyRec));
finally
FS.Free;
end;
// Confirm it's read back in properly
WriteLn('anInt: ', MyRec.anInt, ' aBool: ', MyRec.aBool, ' aByte: ', MyRec.aByte);
ReadLn;
end.