Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-07

how to test if a file is open to write?

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bernd

Because I have no idea how to do this properly (It has been a hundred years since I last used this form of file IO in Pascal) I have done the following hack, just to get it running somehow but now I wonder what would be the proper way to do this. How can I do something like a hypothetical IsOpen(StdOut) the proper way? This is what I have done but I don't like it:

implementation
var
  HasOutput: Boolean = True;
procedure log(const s: String);
begin
  if HasOutput then
    writeln(s);
end;
initialization
  try
    writeln();
  except
    HasOutput := False;
  end;
end.

Reply by email

Ludo Brands
to Bernd
What about using IsConsole? This indicates if the app has been compiled with
{$apptype console}.

Your solution has the advantage of being simple and cross-platform.

On windows this works for me:

function IsConsoleOpen:boolean;
var mode:longword;
begin
result:=GetConsolemode(GetStdHandle(STD_OUTPUT_HANDLE),mode);
end;

Ludo

Show quoted text 37 lines
-----Message d'origine----- De : De la part de Bernd Envoyé : mercredi 13 juillet 2011 21:06 À : FPC-Pascal users discussions Objet : [fpc-pascal] how to test if a file is open to write? Because I have no idea how to do this properly (It has been a hundred years since I last used this form of file IO in Pascal) I have done the following hack, just to get it running somehow but now I wonder what would be the proper way to do this. How can I do something like a hypothetical IsOpen(StdOut) the proper way? This is what I have done but I don't like it: implementation var HasOutput: Boolean = True; procedure log(const s: String); begin if HasOutput then writeln(s); end; initialization try writeln(); except HasOutput := False; end; end. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Bart
to Ludo Brands

There is an IsConsole boolean in the system unit AFAIK.

Bart

Ludo Brands wrote 61 lines
What about using IsConsole? This indicates if the app has been compiled with {$apptype console}. Your solution has the advantage of being simple and cross-platform. On windows this works for me: function IsConsoleOpen:boolean; var mode:longword; begin result:=GetConsolemode(GetStdHandle(STD_OUTPUT_HANDLE),mode); end; Ludo > -----Message d'origine----- > De : > De la part de Bernd > Envoyé : mercredi 13 juillet 2011 21:06 > À : FPC-Pascal users discussions > Objet : [fpc-pascal] how to test if a file is open to write? > > > Because I have no idea how to do this properly (It has been a > hundred years since I last used this form of file IO in > Pascal) I have done the following hack, just to get it > running somehow but now I wonder what would be the proper way > to do this. How can I do something like a hypothetical > IsOpen(StdOut) the proper way? This is what I have done but I > don't like it: > > implementation > var > HasOutput: Boolean = True; > > procedure log(const s: String); > begin > if HasOutput then > writeln(s); > end; > > initialization > try > writeln(); > except > HasOutput := False; > end; > end. > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Ludo Brands
to Bart

That was my first line ;)

Show quoted text 9 lines
There is an IsConsole boolean in the system unit AFAIK. Bart On 7/14/11, Ludo Brands wrote: > What about using IsConsole? This indicates if the app has been > compiled with {$apptype console}.

Reply by email

Michael Müller
to Bernd: Because I have no idea how to do this properly (It has been a hundred years since I last…

Hi Bernd

Bernd wrote 24 lines
Because I have no idea how to do this properly (It has been a hundred years since I last used this form of file IO in Pascal) I have done the following hack, just to get it running somehow but now I wonder what would be the proper way to do this. How can I do something like a hypothetical IsOpen(StdOut) the proper way? This is what I have done but I don't like it: implementation var HasOutput: Boolean = True; procedure log(const s: String); begin if HasOutput then writeln(s); end; initialization try writeln(); except HasOutput := False; end; end.

The subject indicates that you look for a solution for normal files but your example indicates that you what to detect the console. So I add my solutions for both.

function HasConsole: Boolean;
// This function tests if there is a console. If GetStdHandle() reports that there is a console when in fact there
// isn't (as in Matlab R2008a), we still need to check if writing to the output handle works to get the right answer.
begin
 // In Free Pascal V2.4.0 STD_*_HANDLE seem to be defined in a way that it is not directly usable for WIN64 target. In
 // the source rtl\win\syswin.inc, procedure SysInitStdIO() there is Cardinal() (which is the type of STD_*_HANDLE in
 // Delphi) around the handles.
 Result := ((GetStdHandle({$IFDEF WIN64}Cardinal{$ENDIF}(STD_INPUT_HANDLE))   0) and
            (GetStdHandle({$IFDEF WIN64}Cardinal{$ENDIF}(STD_OUTPUT_HANDLE))  0));
 if (Result) then
   try
     write('');
   except
     on EInOutError do begin
       Result := False;
     end;
   end;
end;

For normal files I have:

function FileIOMode(var F: Text): Word;
begin
 case TTextRec(F).Mode of
   fmClosed,
   fmInput,
   fmOutput,
   fmInOut : Result := TTextRec(F).Mode;
   // There is no 'fmUnknown' to indicate this ;-) I assume this mean 'handle never used before'.
   else      Result := fmClosed;
 end;
end;

(Same function for untyped files: replace 'Text' by 'file' and ' TTextRec' by 'TFileRec')

And in the code you can write

If (FileIOMode(Foo)  fmClosed) then
...

Regards

Michael

Reply by email

Bernd
to Michael Müller

2011/7/15 Michael Müller :

The subject indicates that you look for a solution for normal files but your example indicates that you what to detect the console. So I add my solutions for both. [...]

This is helpful information! Normally I would try do do file IO with streams. Only the console problem made me realize again that it seems I have completely forgotten everything I ever knew about the File and Text types and their internal mechanisms.

Bernd

Reply by email

Sven Barth
to Bernd
Bernd wrote 7 lines
2011/7/15 Michael Müller: > The subject indicates that you look for a solution for normal files but your example indicates that you what to detect the console. So I add my solutions for both. [...] This is helpful information! Normally I would try do do file IO with streams. Only the console problem made me realize again that it seems I have completely forgotten everything I ever knew about the File and Text types and their internal mechanisms.

Hmm... I don't know whether I understand your intension correctly, but do you know about the unit "IOStream"?

See here: http://www.freepascal.org/docs-html/fcl/iostream/index.html

Regards,
Sven

Reply by email

Bernd
to Sven Barth

2011/7/15 Sven Barth :

Hmm... I don't know whether I understand your intension correctly, but do you know about the unit "IOStream"?

I know about streams and use all sorts of streams every day, but what i don't know very much about is the old pascal-ish way of handling files that I have not used for ages now (assign(), rewrite() and friends)

Reply by email