[fpc-pascal] Where is the 'write' function defined and how is it different from 'writeln'?

Sven Barth pascaldragon at googlemail.com
Fri Mar 20 21:54:21 CET 2015


On 20.03.2015 21:18, vfclists . wrote:
>
>
> On 20 March 2015 at 19:34, Sven Barth <pascaldragon at googlemail.com
> <mailto:pascaldragon at googlemail.com>> wrote:
>
>     Am 20.03.2015 19:19 schrieb "vfclists ." <vfclists at gmail.com
>     <mailto:vfclists at gmail.com>>:
>
>
>      >
>      >
>      >
>      > On 20 March 2015 at 18:01, leledumbo <leledumbo_cool at yahoo.co.id
>     <mailto:leledumbo_cool at yahoo.co.id>> wrote:
>      >>
>      >> > Where is the 'write'  function defined and how is it different
>     from
>      >> 'writeln'?
>      >> >
>      >> > I can see a lot of fpc_writeXXX and other xxxxWrite functions,
>     but no
>      >> > 'write' itself
>      >>
>      >> those fpc_writeXXX ARE the actual write. Write(Ln) is NOT a
>     function as like
>      >> others whose implementation you can clearly see. It's rather a
>     command for
>      >> the compiler to translate to the correct fpc_writeXXX call. So,
>     if you:
>      >>
>      >> WriteLn(123,' is an integer');
>      >>
>      >> the compiler will translate it to:
>      >>
>      >> fpc_write_text_shortint(123);
>      >> fpc_write_text_shortstring('is an integer');
>      >> fpc_writeln_end;
>      >>
>      >> The same case applies to Read(Ln). AFAIK Pascal's I/O is part of the
>      >> language, not the RTL.
>      >>
>      >>
>      >
>      > Where does the output go? Is it for stdout, strderr  or the console?
>
>     It depends.
>
>     Write('foobar');
>
>     Will write to whatever Textfile is contained in Output and
>
>     Write(xyz, 'foobar');
>
>     Will write to the xyz Textfile. And these Textfiles can basically be
>     anything. By default Output simply writes to StdOut (there's also a
>     variable for StdErr, but I have forgotten how it's called...), but
>     you could also use an implementation that writes to a TStream or one
>     which uses sockets. It's quite flexible...
>
>     Regards,
>     Sven
>
>
> How do you ensure own implementation overrides the system's
> implementation, does the compiler take care of that automatically, or
> will you have to name your function differently?

There is no need to ensure that. Here is an example:

=== code begin ===

var
   f, oldout: TextFile;
begin
   Writeln('Hello Output as StdOut');

   oldout := Output;

   Assign(Output, 'test.txt');
   Rewrite(Output);

   Writeln('Hello Output as file');

   Close(f);

   Output := oldout;

   Writeln('Hello Output as StdOut again');
end.

=== code end ===

To see how such a TextFile is implemented you can take a look at unit 
StreamIO which is part of FPC's units.

(Though I wonder why "Assign(f, 'test.txt'); Output := f; Writeln('Hello 
Output as file');" does not work :/ )

Regards,
Sven



More information about the fpc-pascal mailing list