• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1193
  • Last Modified:

IoResult = 103 when trying to Reset File

I am resetting an input file to read it but I get IoResult = 103 after the operation.  Resetting is not one of the functions listed for 103, so why am I seeing it?

The routine I am using is:

function ResetFile(var LogicName : TextFile;Const FileName : String) : Boolean;
var
  IOR: integer;
begin
   AssignFile(LogicName, FileName);
   {$I- }
   Reset(LogicName);
   // IOR := IOResult;
   {$I+ }
   IOR := IoResult;

   if IOR <> 0 then
      begin
        ResetFile := False;
        ShowMessage('Failed to reset file '+FileName+
                 '. IoResult = '+IntToStr(IOR));
      end
   else
      ResetFile := True;
end;   { ResetFile }

Can you tell me what I am doing wrong?

Thanks,
John Fistere
0
John Fistere
Asked:
John Fistere
  • 2
2 Solutions
 
John FistereEngineer, retiredAuthor Commented:
I have found that regardless of the error message, the file is read properly, so right now I am programmatically ignoring the error.

I sure would like to know why it is cropping up, though.

John Fistere
0
 
TheRealLokiSenior DeveloperCommented:
the {I+} reset and assignfile you are using are only included in delphi for legacy support
you really should be using CreateFile() when possible
e.g.

    var
        ahFile: THandle;
        size: Longword;
    begin
        size := 0;
        if OpenDialog1.Execute then
        begin
            ahFile := CreateFile(PChar(OpenDialog1.Filename), GENERIC_READ,
            FILE_SHARE_DELETE + FILE_SHARE_READ + FILE_SHARE_WRITE, {allow all}
              nil, {security} OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
            try
                size := GetFileSize(ahFile, Nil);
            finally
                FileClose(ahFile);
            end;
            ShowMessage(IntToStr(size));
        end;
    end;

Error 103 means "file not open"
You might want to try opening the file with share access (allowing other processes to access the file also)
something like
function ResetFile(var LogicName : TextFile;Const FileName : String) : Boolean;
var
  IOR: integer;
  oldfilemode: integer; //  store the last value so we can revert it afterwards
begin
   {$I- }
   OldFileMode := filemode;
   filemode := 64; // 0=read, 2= write, 64=read&share, 66=write&share

......
   filemode := oldfilemode;
0
 
Scay7Commented:
With that reset function like...

resetfile(f,test.txt);
while not eof(logicname) do
begin
end;
closefile(f);  <-- this part must be included after every call of resetfile;

mabey you missed one?

   {$I- }
   Reset(LogicName);
   {$I+ }

This part just intraps the reset for an ioresult so that you can read it...
0 = everything is fine
103 = file not open
0
 
John FistereEngineer, retiredAuthor Commented:
Thanks Loki and Gavin for your comments.

In this case I have simply decided to ignore the 103 error.  I will save your answers and do it right for my next project.

I am still running D5 and haven't found a reason to upgrade.  CreateFile() is mentioned as an API call and not documented in my version of Help, so it's a little inconvenient.

Thanks,
John Fistere
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now