node.js: readSync from stdin?

前端 未结 11 1542
北海茫月
北海茫月 2020-12-08 01:48

Is it possible to synchronously read from stdin in node.js? Because I\'m writing a brainfuck to JavaScript compiler in JavaScript (just for fun). Brainfuck supports a read o

11条回答
  •  一整个雨季
    2020-12-08 02:21

    function read_stdinSync() {
        var b = new Buffer(1024)
        var data = ''
    
        while (true) {
            var n = fs.readSync(process.stdin.fd, b, 0, b.length)
            if (!n) break
            data += b.toString(null, 0, n)
        }
        return data
    }
    

提交回复
热议问题