Skip to content

Instantly share code, notes, and snippets.

@tanyagupta
Created December 5, 2015 19:40
Show Gist options
  • Save tanyagupta/d5f80b316102ffdc3251 to your computer and use it in GitHub Desktop.
Save tanyagupta/d5f80b316102ffdc3251 to your computer and use it in GitHub Desktop.
MY FIRST ASYNC I/O! (Exercise 4 of 13)
var fs = require('fs');
var content = fs.readFile(process.argv[2],function(err,data){
if(err){
console.log('error');
}
var lines=data.toString().split('\n');
console.log(lines.length-1);
});
@siddhartha97
Copy link

@Dipanshu27 the code seems to be wrong in this part :

var strFile = bufFile.toString();

The callback (err,data) second parameter data acts as the buffer.

The modified code is :

if(err){
console.log('error');
}
var strFile = data.toString();
var cnt = strFile.split('\n');
console.log(cnt.length - 1);
}); 

Cheers ! 👍

@palaklive
Copy link

palaklive commented Mar 18, 2021

const fs = require("fs");
const file = process.argv[2];

fs.readFile(file, "utf8", function (err, contents) {
  if (err) {
    return console.error(err);
  }
  const lines = contents.split("\n").length - 1;
  console.log(lines);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment