Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:33:53+00:00 2026-06-17T04:33:53+00:00

If I want to read from the input stream in C I write scanf

  • 0

If I want to read from the input stream in C I write scanf., Is there equivalent method in NodeJS to do the same?

For example, here’s the code in C

int n,
    m,
    i;

scanf("%d", &n);

for (i = 0; i < n; i++) {
    scanf("%d", &m);

    ............

}

Here’s where I’m starting from in Node… TODO indicates where I’m stuck:

process.stdin.resume();
process.stdin.setEncoding("ascii");

process.stdin.on("data", function (input) {
    var n = +input;
    for (var i = 0; i < n; i++) {
        // TODO
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:33:54+00:00Added an answer on June 17, 2026 at 4:33 am

    For starters, calling scanf and the data event for a readable stream in NodeJS are not equivalent. In the NodeJS example, you will need to parse the chunk of the input you’ve received.

    You can examine how these chunks are sent to you by replacing the body of your on method with a simple:

    process.stdout.write('onData: ' + input + '\n');
    

    Given how input then contains your data you’ll need to use some method to extract the string of interest and then use parseInt. Perhaps a naive approach to your problem, assuming 1 integer per input:

    var n = 0;
    var m = 0;
    var state = 0;
    process.stdin.on('data', function (input) {
        switch (state)
        {
        case 0:
            // we're reading 'n'
            n = parseInt(input.trim(), 10);
            state++;
            break;
    
        default:
            // we're reading 'm'
            m = parseInt(input.trim(), 10);
    
            if (state++ == n)
            {
                // we've read every 'm'
                process.exit();
            }
            break;
        }
    });
    

    I’m not a terribly large fan of this means of getting data to your NodeJS event loop, you should instead look to command line arguments, configuration/input files, or some other means.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to write bytes and read them from standard input/output
I want to read user input from STDIN and process the preliminary input while
I want to read the file path from html input type=file (the entry selected
Within a VB.NET program, I want to read files from a filesystem, then write
I want to read DOM document using Stax stream readers and write it using
I want to open a process from C code, and be able to read
I am trying to Make a Stream Writer and then write some input from
I want to read from stdin five numbers entered as follows: 3, 4, 5,
i want to read from a text file in C#. But I want all
I want to read lines from a file into a Set or List. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.