Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-04

Detecting IO errors with INI file

9 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I'm trying to detect if an INI file is open by another program, but the way I normally do this is not working.

// open the file for write and keep it open to test
Assign(myfile,'my.ini');
Rewrite(myfile);
{$i-}
Assign(myfile2, 'my.ini';
Rewrite(myfile2);    //try to open it for write again
Writeln(ioresult);    //correctly displays a 5 for access denied
{$i+}
{$i-}
Log_Ini := TIniFile.Create('my.ini');   // blows up with 217
Writeln(ioresult);
{$i+}

The error I get is:
An unhandled exception occurred at $005A57D0:
EFOpenError: Unable to open file "LOG.INI": The process cannot access the file because it is being used by another process.

I want to just keep trying to open it until the other process is done. I don't understand why it's an 'unhandled exception' when I was handling it. I guess TiniFile.Create has an {$i+} in it and so that's why it's blowing up?

Any ideas how to keep retrying to access with ini file until it is either successful or a timeout?

James

Sven Barth
to James Richters

James Richters via fpc-pascal schrieb am
Do., 29. Apr. 2021, 03:13:

Show quoted text 25 lines
{$i-} Log_Ini := TIniFile.Create(‘my.ini’); // blows up with 217 Writeln(ioresult); {$i+} The error I get is: An unhandled exception occurred at $005A57D0: EFOpenError: Unable to open file "LOG.INI": The process cannot access the file because it is being used by another process. I want to just keep trying to open it until the other process is done. I don’t understand why it’s an ‘unhandled exception’ when I was handling it. I guess TiniFile.Create has an {$i+} in it and so that’s why it’s blowing up

TIniFile does not make use of IOResult, because it's from the Delphi time, not the TP time. Thus you need to use Object Pascal exception handling:

=== code begin ===

try
  Log_ini := TIniFile.Create('myini.ini');
except
  on e: EFOpenError do
    // handle this to decide whether to retry
  on e: Exception do
    // handle other problems
end;

=== code end ===

Regards,
Sven

James Richters
to Sven Barth

I get Error: Identifier not found “Try”
Because my unit must be compiled with {$MODE TP}
Otherwise quite a few procedures and functions won’t even compile that only work in Turbo Pascal mode.

I guess I can put it in another unit and make a call to that… then I can use Try

Thanks for the information.

James

Sven Barth
to James Richters

James Richters schrieb am Do.,
29. Apr. 2021, 10:33:

I get Error: Identifier not found “Try” Because my unit must be compiled with {$MODE TP}

You can try to use {$modeswitch exceptions}, I think.

Otherwise quite a few procedures and functions won’t even compile that only

Show quoted text 7 lines
work in Turbo Pascal mode. I guess I can put it in another unit and make a call to that… then I can use Try

Considering that you're using Object Pascal functionality with TIniFile it might be best to put that into a separate unit.

Regards,
Sven

James Richters
to Sven Barth

I’m trying to put the Ini file stuff all in it’s own unit.

My unit is {$Mode OBJFPC}
and Uses IniFiles;

“Try” is compiling, but I’m getting Error: Identifier not found “EFOpenError”

And also Syntax error “Do” expected but “)” found

I have:

try
  Log_ini := TIniFile.Create('myini.ini');
except
  on e: EFOpenError do      // Identifier not found “EFOPenError”
    Writeln(EFOpenError);
  on e: Exception do
    Writeln(Exception);
end;

Do I need some other unit for EFOpenError to work?

James

Michael Van Canneyt
to James Richters
James Richters wrote 21 lines
I’m trying to put the Ini file stuff all in it’s own unit. My unit is {$Mode OBJFPC} and Uses IniFiles; “Try” is compiling, but I’m getting Error: Identifier not found “EFOpenError” And also Syntax error “Do” expected but “)” found I have: try Log_ini := TIniFile.Create('myini.ini'); except on e: EFOpenError do // Identifier not found “EFOPenError” Writeln(EFOpenError); on e: Exception do Writeln(Exception); end; Do I need some other unit for EFOpenError to work?

Best add SysUtils and Classes.

Michael.

James Richters
to Michael Van Canneyt
Best add SysUtils and Classes.

Thanks, that worked. But now I'm getting "cannot read or write variables of this type" for Writeln(Exception) is there an easy way to find out what the exception is?

James

Michael Van Canneyt
to James Richters
James Richters wrote
> Best add SysUtils and Classes. Thanks, that worked. But now I'm getting "cannot read or write variables of this type" for Writeln(Exception) is there an easy way to find out what the exception is?
On E : Exception do
   Writeln(E.ClassName,' : ',E.Message);

Adapt E to whatever variable you used.

Michael.

James Richters
to Michael Van Canneyt

Thanks everyone for the help. I have it working now I did some more searching and found out I can get "Try" to work with {$Mode TP} If I add {$Modeswitch exceptions} and appearantly {$R+} to make sure range checking is on. So I have it working in my {$Mode TP} Unit.