Sign in Join

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

Mailing lists / fpc-pascal / 2008-10

stdout refuses to be redirected

8 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Arjan van Dijk

Hi!

The following small piece of code, with filename 'test.pas':

PROGRAM Test;
USES CRT,DOS;
VAR
  DirInfo: SEARCHREC;
BEGIN
  FINDFIRST('*.pas',ARCHIVE,DirInfo);
  WRITELN(DirInfo.Name);
END.

just gives the name of the first file with extension ".pas". It compiles and runs fine when I call is as follows:

test.exe

Now when I try to redirect the output to a file, then that file remains empty:

test.exe > somefile

This is the narrowed down version of my malfunctioning program. Can someone please have a look into this? A whole lot of interesting calculations (and my boss...) are waiting for this problem to be fixed.

I use:

Free Pascal Compiler version 2.2.2
Compiler Date : 2008/08/03
Compiler CPU Target: i386

Another encounter with similar effects: In the original code I had a loop with FINDNEXT searching a whole directory for datafiles to be processed. This loop terminated after processing 1 file, whereas all files were correctly
listed when I commented out the call to the processing subroutine. This problem is probably related to the above capricious behaviour.

Regards,

Arjan

____________________________________________________________________________

DISCLAIMER: http://www.rivm.nl/disclaimer.htm

Reply by email

Vincent Snijders
to Arjan van Dijk
Arjan van Dijk wrote 26 lines
Hi! The following small piece of code, with filename 'test.pas': PROGRAM Test; USES CRT,DOS; VAR DirInfo: SEARCHREC; BEGIN FINDFIRST('*.pas',ARCHIVE,DirInfo); WRITELN(DirInfo.Name); END. just gives the name of the first file with extension ".pas". It compiles and runs fine when I call is as follows: test.exe Now when I try to redirect the output to a file, then that file remains empty: test.exe > somefile This is the narrowed down version of my malfunctioning program. Can someone please have a look into this? A whole lot of interesting calculations (and my boss...) are waiting for this problem to be fixed.

I have two suggestions, untested.

1. Don't use the crt unit, it grabs the handling of output (writeln(s)
is translated to writeln(output,s);

2. Use writeln(stdout, DirInfo.Name) to bypass crt's handling of output.

Vincent

Reply by email

Arjan van Dijk
to Vincent Snijders
1. Don't use the crt unit, it grabs the handling of output (writeln(s) is translated to writeln(output,s); 2. Use writeln(stdout, DirInfo.Name) to bypass crt's handling of output.

This indeed seems to solve both problems.
Does this really mean that I should replace all WRITELN( statements throughout my code
into WRITELN(STDOUT), ? And probably also all WRITE( statements into WRITE(STDOUT, ?
I always thought that WRITELN; was such a nice, basic instruction.

Thanks!

Arjan

Reply by email

Vincent Snijders
to Arjan van Dijk
Arjan van Dijk wrote 12 lines
> 1. Don't use the crt unit, it grabs the handling of output (writeln(s) > is translated to writeln(output,s); > > 2. Use writeln(stdout, DirInfo.Name) to bypass crt's handling of output. This indeed seems to solve both problems. Does this really mean that I should replace all WRITELN( statements throughout my code into WRITELN(STDOUT), ? And probably also all WRITE( statements into WRITE(STDOUT, ? I always thought that WRITELN; was such a nice, basic instruction.

You can stop using crt, then you don't need to change your write instuctions.

Vincent

Reply by email

Tomas Hajny
Arjan van Dijk wrote 6 lines
> You can stop using crt, then you don't need to change your write > instuctions. And how about my calls to READKEY and KEYPRESSED? Would unit WINCRT bring a solution? Or should I reconsider the use of these two statements anyhow?

You can use alternatives provided by standard RTL unit Keyboard. The point is that the compatibility unit Crt (originally coming from TP/BP legacy) has been supposed to provide enhanced alternatives to basic console input and output provided by unit System, but that implies redefinition of parts of the default functionality.

Tomas

Reply by email

Frank Peelo
to Arjan van Dijk: And how about my calls to READKEY and KEYPRESSED? Would unit WINCRT bring a solution? Or…
Arjan van Dijk wrote 7 lines
>You can stop using crt, then you don't need to change your write >instuctions. And how about my calls to READKEY and KEYPRESSED? Would unit WINCRT bring a solution? Or should I reconsider the use of these two statements anyhow?

One possibility is to assign the output file to ''.

For example, try the following:

uses crt;
Procedure DoIO(s:String);
var
   k : char;
Begin
   writeln(s);
   Writeln('Press keys. Hit Escape to stop.');
   repeat
     k := readkey;
     case k of
     ' '..#126: Write(k);
     #13:       writeln;
     else
       Begin
         sound(440);
         delay(200);
         nosound;
       end;
     end; { case k }
   until k=#27;
end;
Begin
   textcolor(yellow);
   DoIO('First call');
   assign(output, '');
   rewrite(output);
   DoIO('Second call');
end.

If you run this, the first call to DoIO uses the CRT unit. This comes out in colour, but cannot be redirected. The second call goes to stdout. This can be redirected but is not in colour.

(This is at least true in Windows XP and FPC 2.2.2)

Frank

Reply by email