最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Try to use node.js, fs.read - Stack Overflow

programmeradmin1浏览0评论

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 mand 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            
                };}); });

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 mand 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            
                };}); });
Share Improve this question edited Apr 12, 2011 at 1:42 generalhenry 17.3k4 gold badges50 silver badges63 bronze badges asked Apr 6, 2011 at 18:47 cubecube 1,8024 gold badges25 silver badges35 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

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.

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?

发布评论

评论列表(0)

  1. 暂无评论