简体   繁体   English

在Windows中同步读取stdin

[英]Synchronously reading stdin in Windows

I've been doing this to synchronously read the whole stdin data under Linux: 我一直这样做是为了在Linux下同步读取整个stdin数据:

var buffer = fs.readFileSync('/dev/stdin');

This obviously won't work on Windows since there is no /dev/stdin file. 这显然不适用于Windows,因为没有/ dev / stdin文件。 What could I do to achieve the same? 我能做些什么来实现同样的目标?

var size = fs.fstatSync(process.stdin.fd).size;
var buffer = size > 0 ? fs.readSync(process.stdin.fd, size)[0] : '';

The module readline-sync do the job very well. 模块readline-sync可以很好地完成工作。

npm install readline-sync

and then: 接着:

var readlineSync = require('readline-sync');
var answer = readlineSync.question('What is your favorite food? :');
console.log('Oh, so your favorite food is ' + answer);

https://www.npmjs.com/package/readline-sync https://www.npmjs.com/package/readline-sync

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM