0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

stdinとstdout

Last updated at Posted at 2025-11-19

超ざっくりいうと

  • 標準入力(stdin)は、通常はプログラムがキーボードから受け取るデータのことです。
  • 標準出力(stdout)は、プログラムが処理結果などのデータを、通常は画面に表示するための出力先です。
process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  console.log(lines[0]);
});

■ 各行の意味

  1. process.stdin.resume();
    Node.js の標準入力ストリームを「停止 → 再開」して、入力受付状態にする。
  2. process.stdin.setEncoding('utf8');
    入力された文字を UTF-8 として扱う。
  3. var lines = [];
    入力行を保存するための配列。
  4. reader = require('readline').createInterface({...})
    readline モジュールで 行単位の入力を扱う仕組みを作る。
  5. reader.on('line', (line) => { lines.push(line); });
    入力で1行読み込むたびに lines に追加。
  6. reader.on('close', () => { console.log(lines[0]); });
    入力が終了したら(EOF になったら)
    最初の行を表示して処理終了。
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?