Read Large File Line By Line
NodeJS
Hard
3 views
Problem Description
Read a file line by line and print how many lines contain 'meetcode'.
Input Format
stdin: file path.
Output Format
Print integer.
Constraints
Use readline with createReadStream.
Official Solution
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);
}
})();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!