標準入力からの入力を標準出力にコピーする

明解C言語 入門編 > 8. いろいろなプログラムを作ってみよう >

標準入力からの入力を標準出力にコピーする

C


#include <stdio.h>

int main(int argc, char* argv[])
{
int ch;

while ((ch = getchar()) != EOF)
putchar(ch);

return 0;
}

実行結果


R:\>lesson067\Project1.exe
Hello,
Hello,
World!
World!
^Z

Delphi


program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

procedure main();
var
s: String;
begin
while (not Eof) do
begin
readln(s);
writeln(s);
end;
end;

begin
main;
end.

実行結果


S:\>lesson067\Project1.exe
Hello,
Hello,
World!
World!
^Z

Perl
print while(<>);

実行結果

L:\>perl lesson_08_067.pl
Hello,
Hello,
World!
World!
^Z

Ruby
STDIN.each do |line|
  puts line
end

puts ""

puts $s while $s = STDIN.gets

実行結果

L:\>ruby l:\lesson_08_067.rb
Hello,
Hello,
World!
World!
^Z

Hello,
Hello,
World!
World!
^Z