【问题标题】:Reading files after giving path from stdin从标准输入给出路径后读取文件
【发布时间】:2022-06-11 18:22:22
【问题描述】:

通过标准输入定义路径后如何读取文件?我得到一个路径不是字符串的错误。

const path = process.stdin;
const lines = fs.readFileSync(path, "utf-8").split("\n").filter(Boolean);
let numberOfElephants = lines[0];
let massOfElephants = lines[1].split(" ").map((mass) => +mass);

let elephantsOrderGiven = lines[2].split(" ").map((order) => +order);

let proposedOrder = lines[3].split(" ").map((order) => +order);

【问题讨论】:

  • process.stdin 是一个流,你必须读取流并处理给定的数据。 process.stdi 不是字符串。
  • nodejs.org/api/readline.html 应该是你要找的api

标签: node.js