cube
cube

Reputation: 1802

Try to use node.js, fs.read

This is how I am calling fs.read, but I'm continually getting an error. Is there something wrong here with my syntax?

The error on the command line is: "errorCode": -1,

        var fs = IMPORTS.require('fs'), 
    sys = IMPORTS.require("sys")    

    var file=   this.filename, 
    start=  parseInt(offsetStart),
    end=    parseInt(offsetEnd);
    bufSize = 64 * 1024;

     fs.open(file,'r',0666,function(err,fd) {
          fs.read(fd,bufSize,0,end,start,function(err,str,count) {
              result = {    reply:str,
                        reply2:count            
                };}); });

Upvotes: 0

Views: 3872

Answers (2)

Michael Dillon
Michael Dillon

Reputation: 32392

It might help if you explain what you are doing here. Why are you opening a file and what are you trying to read from it?

If it is a textfile it may be simpler to use a ReadStream something like this:

inp = fs.createReadStream('sample.txt');
inp.setEncoding('utf8');
inptext = '';
inp.on('data', function (data) {
    inptext += data;
});
inp.on('end', function (close) {
    console.log(inptext);
});

You might want to look at your code and ask yourself where the data in your return statement goes. If you really want to use a callback chain, you might try passing in an empty object, and then fill that with data, so that you don't have to worry about sending the data back up the callback chain.

Upvotes: 6

Bill IV
Bill IV

Reputation: 205

if you expect up to 100k and the buffer is 64k, and the image is offset, could it be getting the first 57K of something starting around 7k?

What happens if the bufSize is 256 * 1024?

Can the values of offsetStart and offsetEnd be displayed or dumped? They seem worth knowing.

Also, is the second value really an offset, or is it a length?

Upvotes: 1

Related Questions