Created
December 5, 2015 19:40
-
-
Save tanyagupta/d5f80b316102ffdc3251 to your computer and use it in GitHub Desktop.
MY FIRST ASYNC I/O! (Exercise 4 of 13)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
@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 ! 👍
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
hey can you tell me what is the problem in this code
var fs = require('fs'');
var filename = process.argv[2];
var bufFile = fs.readFile( filename,function callback(err, data){
if(err){
console.log('error');
}
var strFile = bufFile.toString();
var cnt = strFile.split('\n');
console.log(cnt.length - 1);
});