![](/img/trans.png)
[英]How to take two consecutive input with the readline module of node.js?
[英]node.js: take input from file using readline and process I/O in windows command prompt
我想使用node.js从命令提示符运行 name.js 文件并传递输入文件并将 output 重定向到 output.txt 中,我正在为此编写一个命令node name.js < input.txt | > output.txt
node name.js < input.txt | > output.txt
但这不起作用或者我错了。
name.js 看起来像这样:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var _line = "";
rl.on('line', function(line){
_line += line;
});
rl.on('pause', function(_line){
console.log(_line);
});
我也在Powershell -Command "command"
中试过这个
编辑:例如 input.txt 包含
hello js
hello node
hello world!
现在,如果我运行node name.js < input.txt > output.txt
。 我只是在 output.txt 中得到console.log() 的“未定义”的返回值
将 _line 传递给rl.on ('pause',function( _line ){} 隐藏全局_line这就是为什么它给出 undefined 并且 cmd 命令很好。
还有其他方法可以通过使用 node.js 的进程 I/O来做到这一点
function YourData(input) {
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
YourData(_input);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.