Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9409

JavaScript(Node.js)での標準入出力について

$
0
0
JavaScript(Node.js)での標準入出力について PaizaのスキルチェックやAtCoderで使う、JavaScript(Node.js)での標準入出力についてまとめてみました。 1. fs.readFileSyncを使った方法 // 入力 // 30 1 3 // 40 56 // 160 90 53 // 6000 const lines = require("fs").readFileSync(0, "utf8").replace(/\n$/, "").split(/\n/); console.log(lines); // 出力 // [ '30 1 3', '40 56', '160 90 53', '6000' ] あとはsplit()で各行を分割するなりお好きに 2. readlineを使った方法 // 入力 // 30 1 3 // 40 56 // 160 90 53 // 6000 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); }); // 出力 // [ '30 1 3', '40 56', '160 90 53', '6000' ] こちらでも1.と同じ配列が作れますが、記述が少なくて済むので1.の方法がおすすめです!

Viewing all articles
Browse latest Browse all 9409

Trending Articles