LoginSignup
14
10

More than 5 years have passed since last update.

node.jsで標準入力

Last updated at Posted at 2014-08-12

概要

atcoderで標準入力に対応した時の話

対象者

  • windowsでやっている

コード

type a.txt | node a.jsで実行します。

パイプを使わないと、endイベントが発生しません。

process.stdin.resume();
process.stdin.setEncoding('utf8');

var g_input = '';
process.stdin.on('data', function(chunk) {
    g_input += chunk;
});
process.stdin.on('end', function() {
    main(g_input.split(/\r?\n/));
});
function main(inputs){
// todo
}

備考

windowではない場合は、次が簡単です。

var stdin = require("fs").readFileSync("/dev/stdin", "utf8");
14
10
0

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
14
10