Read Large File Line By Line

Read Large File Line By Line

Hard NodeJS File System & Paths 25 views
Explanation Complexity

Problem Statement

Read a file line by line and print how many lines contain 'meetcode'.

Input Format

stdin: file path.

Output Format

Print integer.

Example

a.txt
3

Constraints

Use readline with createReadStream.

Input / Output Format

Input Format
stdin: file path.
Output Format
Print integer.
Constraints
Use readline with createReadStream.

Examples

Input:
a.txt
Output:
3

Example Solution (Public)

NodeJS
const fs = require('fs');
const readline = require('readline');
const p = fs.readFileSync(0, 'utf8').trim();
if (!p) process.exit(0);

(async () => {
  let count = 0;
  try {
    const rl = readline.createInterface({ input: fs.createReadStream(p), crlfDelay: Infinity });
    for await (const line of rl) {
      if (line.includes('meetcode')) count += 1;
    }
    console.log(count);
  } catch (e) {
    console.log(0);
  }
})();

Official Solution Code

const fs = require('fs');
const readline = require('readline');
const p = fs.readFileSync(0, 'utf8').trim();
if (!p) process.exit(0);

(async () => {
  let count = 0;
  try {
    const rl = readline.createInterface({ input: fs.createReadStream(p), crlfDelay: Infinity });
    for await (const line of rl) {
      if (line.includes('meetcode')) count += 1;
    }
    console.log(count);
  } catch (e) {
    console.log(0);
  }
})();
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.