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

javascript - Is it possible to rewind a file descriptor cursor in nodejs? - Stack Overflow

programmeradmin2浏览0评论

This is what I would do in a perfect world:

fs.open('somepath', 'r+', function(err, fd) {
    fs.write(fd, 'somedata', function(err, written, string) {
       fs.rewind(fd, 0) //this doesn't exist
    })
})

This is my current implementation:

return async.waterfall([
    function(next) {
      //opening a file descriptor to write some data
      return fs.open('somepath', 'w+', next)
    }, 
    function(fd, next) {
      //writing the data
      return fs.write(fd, 'somedata', function(err, written, string) {
        return next(null, fd)
      })
    },
    function(fd, next) {
      //closing the file descriptor
      return fs.close(fd, next)
    },
    function(next) {
      //open again to reset cursor position
      return fs.open('somepath', 'r', next)
    }
], function(err, fd) { 
   //fd cursor is now at beginning of the file 
})

I tried to reset the position without closing the fd by using:

fs.read(fd, new Buffer(0), 0, 0, 0, fn)

but this throws Error: Offset is out of bounds.

Is there a way to reset the cursor without doing this horrible hack?

/e: The offset is out of bounds error es from this exception. Easily fixed by setting a buffer size to 1 but it does not rewind the cursor. Maybe because we ask the function to read nothing.

This is what I would do in a perfect world:

fs.open('somepath', 'r+', function(err, fd) {
    fs.write(fd, 'somedata', function(err, written, string) {
       fs.rewind(fd, 0) //this doesn't exist
    })
})

This is my current implementation:

return async.waterfall([
    function(next) {
      //opening a file descriptor to write some data
      return fs.open('somepath', 'w+', next)
    }, 
    function(fd, next) {
      //writing the data
      return fs.write(fd, 'somedata', function(err, written, string) {
        return next(null, fd)
      })
    },
    function(fd, next) {
      //closing the file descriptor
      return fs.close(fd, next)
    },
    function(next) {
      //open again to reset cursor position
      return fs.open('somepath', 'r', next)
    }
], function(err, fd) { 
   //fd cursor is now at beginning of the file 
})

I tried to reset the position without closing the fd by using:

fs.read(fd, new Buffer(0), 0, 0, 0, fn)

but this throws Error: Offset is out of bounds.

Is there a way to reset the cursor without doing this horrible hack?

/e: The offset is out of bounds error es from this exception. Easily fixed by setting a buffer size to 1 but it does not rewind the cursor. Maybe because we ask the function to read nothing.

Share Improve this question edited Jul 24, 2015 at 13:58 soyuka asked Jul 24, 2015 at 9:08 soyukasoyuka 9,1254 gold badges42 silver badges55 bronze badges 2
  • 1 Why do not you use the argument "position" functions read / write? – stdob-- Commented Jul 24, 2015 at 9:52
  • 1 I don't need them, I'm using this function to write and position argument is optional. I just want to write some data in a file and get back a file descriptor with a cursor at position 0. – soyuka Commented Jul 24, 2015 at 10:06
Add a ment  | 

3 Answers 3

Reset to default 1

Today, the answer is that it's not in the core, and that it can't be added with pure javascript.

There is an extension node-fs-ext that adds a seek function to move the fd cursor. This is done in C++ here.

Related Stackoverflow question.

NodeJS v6.5.0 has the createReadStream method which accepts an array of options. Among those options are the start and end properties. These properties determine from and to which byte should be read, respectively.

So, if you set start to 0, it will read the file from the first byte. Leaving end empty in that case will cause the stream to read the file all the way to the end.

As an example:

fs.createReadStream('myfile.txt', {start: 0})

Using this read stream would allow you to read the whole file.

fs.write might be helpful here:

  1. fs.open to get fd
  2. fs.write(fd, string, position)
  3. fs.close(fd)
发布评论

评论列表(0)

  1. 暂无评论