Solved

"globally" closing all open file handles?

Posted on 2002-06-05
7
412 Views
Last Modified: 2010-04-04
Hi...
  Is there a statment that will do a "global" close of ALL open file handles in one shot?
  In other words, if I have several files open via this way:

assignfile(infile1, c:\tempfile1.txt');
reset(infile1);
assignfile(infile2, c:\tempfile2.txt');
reset(infile2);
assignfile(infile3, c:\tempfile3.txt');
reset(infile3);
assignfile(infile4, c:\tempfile4.txt');
reset(infile4);

Is there a quick way to "close" them all at once, instead of having to individually close each one.

Thanks
   Shawn
0
Comment
Question by:aztec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7058692
If you cannot place the infile1 .. N in an array then switch over to TFileStreams. A simple for loop will do the work then.
0
 

Author Comment

by:aztec
ID: 7058929
but the file handles  are not named infile1...N - that was only an example. they are completely different names.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 7060834
you could use an array, just the same


var
  FileArray: Array of File;
  i: Integer;
  Str1: String;
begin
SetLength(FileArray,5);
assignfile(FileArray[0], c:\sempfile.txt');
reset(FileArray[0]);
Readln(FileArray[0],Str1);
assignfile(FileArray[1], c:\hempfile.txt');
reset(FileArray[1]);
assignfile(FileArray[2], c:\dempfile.txt');
reset(FileArray[2]);
assignfile(FileArray[3], c:\mempfile.txt');
reset(FileArray[3]);
assignfile(FileArray[4], c:\rempfile.txt');
reset(FileArray[4]);


for i := 0 to 4 do
  CloseFile(FileArray[i]);
end;

in your code use the FileArray[1] instead of a variable name
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7060840
I think he is looking for a way to look for globally open file handles... (application-wide) so that when the application terminates (perhaps even if abnormally?) it will still release all the open (locked?) files.

:)
0
 

Author Comment

by:aztec
ID: 7062603
Dragonslayer - Exactly!!

Is there something that can do this?
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 50 total points
ID: 7063258
I'm not to sure what's needed here but I don't know of any thing to get Files or TextFiles, except by their variable names or pointers to the variable names.  I used the method for TypeCasting the FileArray[0] with TTextRec(FileArray[0]).mode to determine if the Text file is open or closed. If you use an Array of File instead of of an Array of TextFile you can TypeCast with TFileRec( ). I use arrays alot with file searches, like the example below. In the FormDestroy procedure is my version of a Make sure all files are closed.


{Memo1 gets the Text output}
private
    { Private declarations }
    FileArray: Array of TextFile;


procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
{this is for an abnormal program end to be sure all files are closed,
the mode check will prevent IO exceptions}
for i := 0 to High(FileArray) do
if not ((TTextRec(FileArray[i]).Mode and fmClosed) = TTextRec(FileArray[i]).Mode) then
  CloseFile(FileArray[i]);
end;

procedure TForm1.but_OpenFClick(Sender: TObject);
  var
  FS, i: Integer;
  SRec: TSearchRec;
  Dir, S: String;
begin
{this button click searches a Folder and gets the text files}
Dir := 'C:\Stuff\';
FS := FindFirst( Dir + '*.txt' , faHidden , SRec ) ;
if FS = 0 then
  begin
  try
    SetLength(FileArray,1);
    AssignFile(FileArray[0], Dir + SRec.Name);
    Reset(FileArray[0]);
    Read(FileArray[0], S);
    Label1.Caption := SRec.Name;
    while FindNext(SRec) = 0 do
      begin
      SetLength(FileArray,Length(FileArray)+1);
      AssignFile(FileArray[High(FileArray)], Dir + SRec.Name);
      end;
    finally
    FindClose(SRec) ;
    end;
  end;

Label1.Caption := IntToStr(Length(FileArray));
for i := 0 to High(FileArray) do
  begin
  Memo1.Text := Memo1.Text+#13#10+TTextRec(FileArray[i]).Name;
  {I added the Name to show you how to get the file name after AssignFile}
  if (TTextRec(FileArray[i]).Mode and fmClosed) = TTextRec(FileArray[i]).Mode then
  Reset(FileArray[i]);
  ReadLn(FileArray[i], S);
  Memo1.Text := Memo1.Text+#13#10+S;
  if i = 0 then
  CloseFile(FileArray[i]);
  {only one file is closed to test the mode check}
  end;
end;

procedure TForm1.but_CloseFClick(Sender: TObject);
var
i: Integer;
begin
{this button click just does the close file and then checks to be sure the file is closed}
for i := 0 to High(FileArray) do
if not ((TTextRec(FileArray[i]).Mode and fmClosed) = TTextRec(FileArray[i]).Mode) then
  begin
  CloseFile(FileArray[i]);
  Memo1.Text := Memo1.Text+#13#10+'Closed file '+IntToStr(i);
  end;
Memo1.Text := Memo1.Text+#13#10+'NEW';
for i := 0 to High(FileArray) do
if not ((TTextRec(FileArray[i]).Mode and fmClosed) = TTextRec(FileArray[i]).Mode) then
  begin
  Memo1.Text := Memo1.Text+#13#10+'Open '+IntToStr(i);
  end
else
  begin
  Memo1.Text := Memo1.Text+#13#10+'Closed '+IntToStr(i);
  end;
end;
0
 

Author Comment

by:aztec
ID: 7084847
That TTextRec.mode is just what I need. Thanks Slick812.

Cheers
   Shawn
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question