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:

  1. why output messy rather valid part.
  2. how output valid part.
  3. 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

  1. the output messy because buffer prints entire memory address assign on line

    var buf = new buffer(1024); 
  2. to output valid part put limitaion on buffer.tostring method

    console.log(buffer.tostring('utf-8' , 0 , bytesread)); 
  3. yes in right way.

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -