Input of Information into Javascript using terminal

前端 未结 2 2026
时光取名叫无心
时光取名叫无心 2021-01-23 08:48

I want to take the output of a c++ program and input it into the stdin of a javascript file. However I have been unable to push anything into the stdin using the method...

相关标签:
2条回答
  • 2021-01-23 09:11

    This is how I process data from STDIN in Node:

    function readStream(stream, callback) {
      var data = '';
      stream.setEncoding('utf-8');
      stream.on('data', onData);
      stream.on('end', onEnd);
    
      function onData(chunk) {
        data += chunk;
      }
    
      function onEnd() {
        stream.removeListener('end', onEnd);
        stream.removeListener('data', onData);
        callback(data);
      }
    }
    

    The error message in your questions (the "undefined is not a function") error means you were trying to call a method on stdin that doesn't exist. I couldn't find a mention of setRawMode in a cursory scan of the docs. Perhaps you are using an outdated API? I recall having to unpause STDIN (old-style streams) is deprecated.

    0 讨论(0)
  • 2021-01-23 09:12

    When running your program with redirected stdin, you're connected to a ReadStream, not a TTY, so TTY.setRawMode() isn't supported.

    setRawMode() is used to set a tty stream so that it does not process its data in any way, such as providing special handling of line-feeds. Such processed data is referred to as being "cooked".

    Standard node ReadStreams are, by definition, already "raw" in that there is no special processing of the data.

    So, refactor your code without the call to setRawMode() and it should work fine.

    0 讨论(0)
提交回复
热议问题