为什么在fs.write中“错误的参数”错误

好。 所以我试图从一个文件中读取一个数字,代表一个计数器,做一些循环,每次写入第二个文件,然后停止,只要我觉得,并将计数器的新值写入相同的第一个文件。

var textPath = "/kolodvori.txt"; var countPath = "/dijestao.txt"; var buffer = new Buffer(0); fs.readFile(countPath, function (err,data){ if (err) console.log(err); else { console.log(data); i = data; var stream2 = fs.createWriteStream(textPath, {flags: 'a'}); stream2.once('open', function (fd){ fs.open(countPath,'w', function (fd2){ getPageHtml(defHost, firstNewPath, i, function (html,count,callback){ if (count%10==0) console.log(count); ++count; i = new Buffer(count.toString()); //console.log('i:' + i); fs.write(fd2, i, 0, i.length, null, function (err,len,buff){ console.log(err); }); var newPath = defPath.replace(/xxxKOLO1xxx/gi, count); getPageHtml(defHost, newPath, count, callback); }); }); }); } }); 

毕竟我的辛勤工作,我得到了回报

 fs.js:513 binding.write(fd, buffer, offset, length, position, wrapper); ^ TypeError: Bad argument at Object.fs.write (fs.js:513:11) 

在这种情况下,“坏论点”甚至意味着什么? 这与我有两种阅读方式然后写入同一个文件有什么关系吗?

path.join(__dirname, "./kolodvori.txt"使用path.join(__dirname, "./kolodvori.txt" )和path.join(__dirname, "./dijestao.txt")

使用/意味着相对于root ,并且您想要相对于脚本的目录。

记得做var path = require("path"); 在顶部。

我有相同的错误,并最终使用writeFile。

https://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback