Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uwm.edu!bionet!agate!ucbvax!bloom-beacon!eru!hagbard!sunic!mcsun!hp4nl!dutrun!winfave From: winfave@dutrun.UUCP (Alexander Verbraeck) Newsgroups: comp.lang.pascal Subject: Re: Cursor On/Off Code plus ... Message-ID: <15647@dutrun.UUCP> Date: 17 Apr 91 10:59:32 GMT References: <1991Apr8.192802.27683@noose.ecn.purdue.edu> Reply-To: winfave@dutrun.tudelft.nl.UUCP (A.Verbraeck) Organization: Delft University of Technology, The Netherlands Lines: 67 The FileExists function as posted in the "Cursor On/Off Code Plus" posting (originating from Dave Sisson) contains a slight but extremely boresome error, as far as I can see. My first attempt to write a FileExists function contained the same error, and it took me quite a while to find it. The original FE routing looked something like: function FE(n:string):boolean; {a little bit abbreviated} var f:text; begin {$I-} assign(f,n); reset(f); if IOresult<>0 then FE:=false else FE:=true; {$I+} end; When I include the above function in a program like: program TestFE; var i : integer; function FE { as above } begin writeln; for i:=1 to 40 do if FE('C:\CONFIG.SYS') then write('E') else write('N'); writeln; end. The output of this little program is (when C:\CONFIG.SYS exists): EEEEEEEEEEEEEEENNNNNNNNNNNNNNNNNNNNNNNNN which means that the file existed the first 15 times, and not anymore afterward. What's the flaw? Not closing the file in FileExists, which occupies a DOS file handler when it exists. What's the solution? Either always closing the file between the {$I-} and {$I+} directives, or closing it after testing if IOresult reports that the file exists and THEN closing the file. I used the first method, resulting in the following function: function FileExists(fn:string) : boolean; var f : text; begin {$I-} assign(f,fn); reset(f); close(f); FileExists:=(IOresult=0); {$I+} end; Sincerely, ------------------------------------------------------------------------- Alexander Verbraeck e-mail: winfave@duticai.tudelft.nl Delft University of Technology winfave@hdetud1.bitnet Department of Information Systems winfave@duticai.uucp PO Box 356, 2600 AJ The Netherlands winfave@dutrun.tudelft.nl Tel: +31 15 783805 Fax: +31 15 786632/787022 winfave@dutrun.uucp -------------------------------------------------------------------------