Copy stdin to stdout: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Algol 68)
(→‎{{header|ALGOL 68}}: Remove spurious declaration)
Line 11: Line 11:
on logical file end( stand in, ( REF FILE f )BOOL: at eof := TRUE );
on logical file end( stand in, ( REF FILE f )BOOL: at eof := TRUE );
# copy stand in to stand out #
# copy stand in to stand out #
STRING line;
WHILE STRING line; read( ( line, newline ) ); NOT at eof DO write( ( line, newline ) ) OD
WHILE STRING line; read( ( line, newline ) ); NOT at eof DO write( ( line, newline ) ) OD
END</lang>
END</lang>

Revision as of 16:01, 11 November 2018

Copy stdin to stdout is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Create an executable file that copies stdin to stdout, or else a script that does so through the invocation of an interpreter at the command line.

ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.8.3.win32

<lang algol68>BEGIN

   BOOL at eof := FALSE;
   # set the EOF handler for stand in to a procedure that sets "at eof" to true #
   # and returns true so processing can continue                                #                               
   on logical file end( stand in, ( REF FILE f )BOOL: at eof := TRUE );
   # copy stand in to stand out                                                 #
   WHILE STRING line; read( ( line, newline ) ); NOT at eof DO write( ( line, newline ) ) OD

END</lang>

AWK

Using the awk interpreter, the following command uses the pattern // (which matches anything) with the default action (which is to print the current line) and so copy lines from stdin to stdut. <lang AWK>awk "//"</lang>

C

<lang C>

  1. include <stdio.h>

int main(){

 char c;
 while ( (c=getchar()) != EOF ){
   putchar(c);
 }
 return 0;

} </lang>

Perl

<lang perl> perl -pe </lang>

Perl 6

When invoked at a command line: Slightly less magical than Perl / sed. The p flag means automatically print each line of output to STDOUT. The e flag means execute what follows inside quotes. ".lines" reads lines from the assigned pipe (file handle), STDIN by default.

<lang perl6>perl6 -pe'.lines'</lang>

When invoked from a file: Lines are auto-chomped, so need to re-add newlines (hence .say rather than .print) <lang perl6>.say for lines</lang>

Prolog

<lang Prolog> %File: stdin_to_stdout.pl

- initialization(main).

main :- repeat, get_char(X), put_char(X), X == end_of_file, fail. </lang>

Invocation at the command line (with Swi-prolog): <lang sh> swipl stdin_to_stdout.pl </lang>

sed

<lang sh> sed -e </lang>