Synchronously reading stdin in Windows

北城以北 提交于 2019-12-18 09:04:11

问题


I've been doing this to synchronously read the whole stdin data under Linux:

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

This obviously won't work on Windows since there is no /dev/stdin file. What could I do to achieve the same?


回答1:


The module readline-sync do the job very well.

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




回答2:


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


来源:https://stackoverflow.com/questions/8452957/synchronously-reading-stdin-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!