Hi War Robe,
Turbo Pascal... that takes me back... IIRC (I haven't used Turbo 7 since about 1996, so I could be wrong), it has no built in deleteFile or fileExists functions, so heres some I made a long time ago. I think these are right, as I said, its a long time since I used Turbo:-
Code:
type
string255 = string[255];
procedure deleteFile(fileName:string255);
var
f : file of byte;
r : integer; (* Type of this could be wrong, adjust accordingly *)
begin
assign(f,filename);
{$I-}
erase(f);
{$I+}
r:=IOResult;
end;
function fileExists(fileName:string255):
var
f : file of byte;
r : integer; (* Again, the type could be wrong *)
begin
assign(f,filename);
{$I-}
reset(f);
{$I+}
result:=(IOResult=0);
{$I-}
close(f);
{$I+}
r:=IOResult;
end;
I'm guessing, you've not used too many compiler directives yet, so heres a quick explanation of how these routines work without resulting in a run-time error.
Normally, when an I/O error occurs (file doesn't exist when you open it with reset for example) the program will bomb out with an error dialogue. If that happens, these functions are useless, so we need to turn off I/O error checking temporarily. To do this, you use the {$I-} compiler directive. This turns off I/O error checking. When you do this, you MUST call IOResult to get the error code (if any) that was returned (IIRC, these codes are the same as the I/O Error message code that might normally be displayed). If you don't call IOResult, and you turn I/O error checking on with the {$I+} compiler directive, the next I/O operation will fail.
So, in the case of deleteFile, we turn off error checking and use the 'erase' function. If the file exists, it is deleted. If not, nothing happens. We turn on error checking and then clear the I/O error result buffer by calling IOResult. In the case of fileExists, we turn off error checking, and attempt to open the file. If it exists, the function succeeds and IOResult should return 0. If it doesn't exist, the function fails and IOResult will return a non-zero value. We turn error checking on, check the IOResult value. And just in case we did open the file, we turn off error checking, close the file, turn error checking back on and then read IOResult again to clear the result buffer.
I hope this helps.
Bookmarks