An EInOutError exception will be raised only if I / O checking is enabled. To verify that it is turned on, follow these steps:
- In the parameters of your project in "Delphi Compiler Parameters", check the checkbox of input / output
- Remove any {$ IOCHECKS OFF} or {$ I-} directives from your code as they disable I / O checking
This should give you the correct exception if the file does not exist.
Now, if (for some reason) you cannot enable I / O checking:
If I / O checking is disabled, you will not get an EInOutError if something goes wrong. Instead, you should check the IOResult value after each I / O operation. This is similar to the old days of Pascal: if IOResult <> 0 , then an error occurred. This (slightly adapted) excerpt from Delphi docs shows how to work with IOResult:
AssignFile(F, FileName); {$I-} Reset(F); {$I+} if IOResult = 0 then begin MessageDlg('File size in bytes: ' + IntToStr(FileSize(F)), mtInformation, [mbOk], 0); CloseFile(F); end else MessageDlg('File access error', mtWarning, [mbOk], 0);
However, for now, you should use TFileStream to access / create files and no longer use the old Pascal routines. An example of how this might look:
filename := '\klienci\'+linia_klient[id]+'.txt'; if not FileExists(filename) then // "Create a file with the given name. If a file with the given name exists, open the file in write mode." fs := TFileStream.Create(filename, fmCreate) else // "Open the file to modify the current contents rather than replace them." fs := TFileStream.Create(filename, fmOpenReadWrite);
source share