LoginSignup
0

More than 5 years have passed since last update.

PHPとNode.jsの標準入力

Last updated at Posted at 2018-11-18

PHP

<?php

fscanf(STDIN, "%d", $a);
fscanf(STDIN, "%d %d", $b, $c);
fscanf(STDIN, "%s", $s);
echo sprintf("%d %s", ($a + $b + $c), $s).PHP_EOL;

$s = trim(fgets(STDIN)); などもある

Javascript

const main = (input) => {
    input = input.split("\n")
    tmp = input[1].split(" ")
    const a = parseInt(input[0], 10)
    const b = parseInt(tmp[0], 10)
    const c = parseInt(tmp[1], 10)
    const s = input[2]
    console.log("%d %s", a + b + c, s)
}
main(require("fs").readFileSync("/dev/stdin", "utf-8"))

手元で実行するときはEOFを入力しないといけないので
Ctrl + D で終わらせる

*追記 : windowsだと上記のNode.jsの入力は動作しません! 参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0