fs.write函数参数里面position的作用
我写的函数:
content = new Buffer content
fs.open src, 'a', (err, fd)->
throw err if err
position = 0
fs.write fd, content, 0, content.length, position, (err)->
throw err if err
fs.closeSync fd```
但是无论position设值为多少,他都把内容插入到文件的最后,position好像不起作用,
我是想把内容插入到文件的头部,请问应该怎么做?
2 回复
writeToFileFromTop: (src, content)->
content = new Buffer content
fs.open src, 'a', (err, fd)->
throw err if err
position = 0
fs.write fd, content, 0, content.length, position, (err)->
throw err if err
fs.closeSync fd
我已经找到解决办法了,就是把open的flag改为’r+’,具体参见: http://stackoverflow.com/questions/7369861/why-is-fs-write-string-not-documented-in-node-js-docs