Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TextFile type, checking if assigned?

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi

I've got (using D5)

logfile : TextFile;

In my prog. I've got a procedure which is called many times and writes and entry to logfile. I'd like to use the same procedure before logfile has been assigned to a file as the proc does other stuff I'm interested in.

How can I check if logfile has been assigned to a file or not?

I've tried the obvious types but always get
"Operator not applicable for this operand type"

lou

 
I can only think of one dirty way:
Code:
try
  Rewrite(logFile);
  ShowMessage('logFile assigned');
except
  ShowMessage('logFile not assigned');
end;

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
hi Clive

Thanks for the reply, problem with your suggestion is the case of it being assigned and I'll keep clearing the file when I want to add to it.

Here's the code where I want to put the check:-

procedure TForm_Main.ScrollWindowWrite(Msg: string);
var OutString : string
begin
Application.processmessages;
OutString:=formatDateTime('dd/mm/yy hh:nn:ss',Now) + msg;
ScrollWindow.Lines.Add(OutString);
{ if assigned(log) then //doesn't work
begin
Writeln(log,OutString);
flush(log);
end;
} Application.processmessages;
end;

lou

 
p.s. I have used 'logfile' in the original example but it is 'log : Textfile;' in the code.

 
Of course, how stupid of me. Try this instead:
Code:
function FileAssigned(var AFile: TextFile): Boolean;
begin
  Result := True;
  try
    Append(AFile);
  except
    Result := False;
  end;
end;
Usage:
Code:
procedure TForm_Main.ScrollWindowWrite(Msg: string);
var   OutString : string
begin
  Application.processmessages;
  OutString:=formatDateTime('dd/mm/yy hh:nn:ss',Now) + msg;
  ScrollWindow.Lines.Add(OutString);
  if FileAssigned(log) then  //should work!
  begin
    Writeln(log,OutString);
    flush(log);
  end;
  Application.processmessages;
end;

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
hi Clive

I am going steal your little functionette.... [smile2]

big ta for your suggestion
lou

 
Go for it...I just thru it together for this post...so you'll need to check it doesn't have any adverse effects on your file. It shouldn't do because Append, by nature, just readies the file for appending to the end, rather than my first suggestion which wiped it clean!!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Or just make the log file a global variable, assign and open it when your form/application opens, close it when your form/application closes. That way you can just write to it any time you want.
 
hi KempCGDR

I'm afraid I can't do it that way, as the log filename is determined by different factors generated from different processes and I don't know them straight away.

lou

 
If you use a TFileStream instead of a TextFile, you can check
Code:
Assigned

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top