node.js - fs.readFile().toString terminated by empty string -
the following node script, (wc.js) return value wich different unix utility wc;
fs = require('fs'); //console.log(fs.readfilesync(process.argv[2]).tostring().split('\n') ); console.log(fs.readfilesync(process.argv[2]).tostring().split('\n').length );
the output on itself, is:
vagrant@precise32:~/stuff$ wc -l wc.js 3 wc.js vagrant@precise32:~/stuff$ node wc.js wc.js 4
and printing array, seems file get's terminated empty string:
vagrant@precise32:~/stuff$ node wc.js wc.js [ 'fs = require(\'fs\');', 'console.log(fs.readfilesync(process.argv[2]).tostring().split(\'\\n\') );', '//console.log(fs.readfilesync(process.argv[2]).tostring().split(\'\\n\').length );', '' ]
is expected behavior? cannot see reported in node documentation.
are sure don't have trailing newline character @ end of file?
$ node wc.js wc.js [ 'fs = require(\'fs\');', 'console.log(fs.readfilesync(process.argv[2]).tostring().split(\'\\n\') );', '// console.log(fs.readfilesync(process.argv[2]).tostring().split(\'\\n\').length );' ]
you can ignore last newline character .replace(/\n$/, '')
, believe behavior of wc
.
Comments
Post a Comment