Nodejs filesystem.write 位置似乎没有正常工作
Nodejs filesystem.write position doesn't seem to be working properly
我有以下问题,这里是感兴趣的代码位:
var fs = require('fs');
var fd = fs.openSync('path to file', 'a+');
var buffer = new Buffer('Hello Text');
fs.writeSync(fd, buffer, 0, buffer.length, 42)
fs.close(fd);
据官方文档告诉我函数应该这样调用:https://nodejs.org/api/fs.html#fs_fs_writesync_fd_buffer_offset_length_position
根据常识和无数次尝试告诉我位置“42”或我选择放置在那里的任何位置意味着我的 writeSync
应该从位置 42 开始写作。但是,无一例外,它开始恰好在文件末尾写一个换行符。
我什至尝试使用 "legacy" 函数 fs.writeSync(fd, 'hello string', 42, 'utf-8');
,但我仍然得到完全相同的结果。
如文档中所写:
On Linux, positional writes don't work when the file is opened in
append mode. The kernel ignores the position argument and always
appends the data to the end of the file.
我有以下问题,这里是感兴趣的代码位:
var fs = require('fs');
var fd = fs.openSync('path to file', 'a+');
var buffer = new Buffer('Hello Text');
fs.writeSync(fd, buffer, 0, buffer.length, 42)
fs.close(fd);
据官方文档告诉我函数应该这样调用:https://nodejs.org/api/fs.html#fs_fs_writesync_fd_buffer_offset_length_position
根据常识和无数次尝试告诉我位置“42”或我选择放置在那里的任何位置意味着我的 writeSync
应该从位置 42 开始写作。但是,无一例外,它开始恰好在文件末尾写一个换行符。
我什至尝试使用 "legacy" 函数 fs.writeSync(fd, 'hello string', 42, 'utf-8');
,但我仍然得到完全相同的结果。
如文档中所写:
On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.