javascript - the ouput of the buffer is messy -
here scene:
a.js:
something new!
the code:
var fs = require('fs'); var filename = 'a.js'; fs.open(filename, 'r', function (error, fd) { var buf = new buffer(1024); fs.read(fd, buf, 0, buf.length, null, function (error, bytesread, buffer) { console.log(buffer.tostring()); }); });
the output messy.
so here questions:
- why output messy rather valid part.
- how output valid part.
- the third parameter of callback in fs.read second parameter of fs.read function. right?
you should limit length print buffer given here nodejs buffer
the output messy because buffer prints entire memory address assign on line
var buf = new buffer(1024);
to output valid part put limitaion on
buffer.tostring
methodconsole.log(buffer.tostring('utf-8' , 0 , bytesread));
- yes in right way.
Comments
Post a Comment