Reputation: 4872
Ok. So I'm trying to read a number from one file, which represents a counter, do some looping and each time write to a second file and then stop whenever I feel like and write the new value of the counter to the same first file.
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);
});
});
});
}
});
After all my hard work I'm rewarded with a
fs.js:513
binding.write(fd, buffer, offset, length, position, wrapper);
^
TypeError: Bad argument
at Object.fs.write (fs.js:513:11)
What does "bad argument" even mean in this context? Has it something to do with me having 2 ways of reading and then writing to the same file?
Upvotes: 1
Views: 3444
Reputation: 179
I had the same error and ended up using writeFile.
https://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_options_callback
Upvotes: 1
Reputation: 48476
Use path.join(__dirname, "./kolodvori.txt"
) and path.join(__dirname, "./dijestao.txt")
instead.
Using /
means relative to the root
, and you want to be relative to your script's directory.
Remember to do var path = require("path");
at the top.
Upvotes: 2