When I am trying to write in file data:
var data = "Hello"
fs.open(path, 'a', 0666, function(err, fd) {
fs.writeSync(fd, data);
});
and it writes duplicate in file:
HelloHello
Why it writes the same twice?
When I am trying to write in file data:
var data = "Hello"
fs.open(path, 'a', 0666, function(err, fd) {
fs.writeSync(fd, data);
});
and it writes duplicate in file:
HelloHello
Why it writes the same twice?
You're currently opening the file in append mode (that's what the 'a'
stands for). This mode means any data you write will be appended to the END of the file.
So if you run this script twice, you'll see the results you're getting: HelloHello
. If you delete your file then re-run the script one time, you'll see the correct results.
If you'd like to write this data only once, you should probably use the 'w'
mode (write mode) -- this will write whatever you want into the file, destroying whatever was previously there.