Recent

Author Topic: Checking File is Open  (Read 5214 times)

samoraj

  • New Member
  • *
  • Posts: 22
Checking File is Open
« on: July 10, 2017, 01:16:04 am »
how i checking fail open or close i'ts it ?
I tried this, but it always shows me that the file is closed.

AssignFile(F, 'C:\Users\Public\print\all\all.csv');
    {$I-}
    Reset(F);
    {$I+}
  if IOResult = 0 then begin
     ShowMessage('file i'ts closed');
     end;
  if IOResult <> 0 then begin
     ShowMessage(''file i'ts open');
end;                                 

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Checking File is Open
« Reply #1 on: July 10, 2017, 02:34:29 am »
Here it works like it should ...  MessageWindow shows OPEN...  :)
Do you know TFileStream ???

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.   Var
  3.    F: TextFile;
  4.  Begin
  5.   AssignFile(F, 'I:\(DOWNLOADS)\Text.txt');
  6.    {$I-}
  7.     Reset(F);
  8.    {$I+}
  9.  
  10.   If IOResult = 0
  11.   Then ShowMessage('OPEN')
  12.   Else ShowMessage('CLOSED');
  13.  End;

EDIT: Maybe a
Code: Pascal  [Select][+][-]
  1. CloseFile(F);
or
Code: Pascal  [Select][+][-]
  1. Close(F);
at the end is a good idea...  never really used this ... :)
« Last Edit: July 10, 2017, 02:51:08 am by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Checking File is Open
« Reply #2 on: July 10, 2017, 02:49:27 am »
I tried this, but it always shows me that the file is closed.
The reason your code fails is that you read IOresult twice (instead of using the else clause). That is, besides the fact that your code interpret the status wrongly.

The documentation on IOresult states:
Quote
IOresult contains the result of any input/output call, when the {\$i-} compiler directive is active, disabling IO checking. When the flag is read, it is reset to zero. If IOresult is zero, the operation completed successfully. If non-zero, an error occurred.

Ergo: the first if IOResult statement reads the IOResult flag (which is then reset to zero), so that your second if IOResult (will always) returns zero (again).

That is also the reason why user RAW's code works. That code reads the IOResult flag once.
« Last Edit: July 10, 2017, 02:54:17 am by molly »

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Checking File is Open
« Reply #3 on: July 10, 2017, 02:59:35 am »
 :D Yeah.. 0 = Success !!!!
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Checking File is Open
« Reply #4 on: July 10, 2017, 03:07:51 am »
:D Yeah.. 0 = Success !!!!
Yups. I initially overlooked that because the alarm bells were ringing for reading the IOResult flag twice. So, i did not even consider that TS was using it wrongly.

Not judging TS but i'm always amazed to see simple code being able to do everything wrong what possibly could be done wrong. The only thing that could have made it worse is using the {$I } switch the wrong way around  :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Checking File is Open
« Reply #5 on: July 11, 2017, 11:14:07 am »
:D Yeah.. 0 = Success !!!!
Naught errors?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Checking File is Open
« Reply #6 on: July 11, 2017, 08:09:10 pm »
Quote
Naught errors?
Almost...  :D
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018