Languages
[Edit]
EN

Node.js - read process.stdin using fs.readSync() function (sync reading)

8 points
Created by:
Saim-Mccullough
718

In this short article, we would like to show a simple way how to read data from process.stdin using fs.readSync() function in Node.js.

Quick solution:

Where:

  • buffer contains read bytes from process.stdin,
  • readSync() reads bytes and puts them to buffer starting from 0 index in buffer,
  • count contains number of read bytes.

 

Practical example

Edit

In this section you can find simple reusable function that lets to read exact number of bytes from process.stdin.

Hint: consider to use async API or standard input events.

 

Legacy version

Edit

In this section, you can fund unified version that works the same way under Window and Unix/Linux.

 

See also

Edit
  1. Node.js - write buffer into process.stdout using fs.writeSync() function

References

Edit
  1. process.stdin - Node.js API
  2. fs.readSync() - Node.js API

Alternative titles

  1. Node.js - read process.stdin using fs.readSync() method
  2. Node.js - sync read operation from process.stdin
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join