Answer the question
In order to leave comments, you need to log in
How to write a string to the middle of a file in NodeJS?
Is it possible to write a line in the middle of a file without overwriting the entire file?
For example, there is a test.txt file with the content: 1234567890abcdfghijklmnoprstxyz
You need to write the string "++" instead of "ab", that is, what would happen: 1234567890++cdfghijklmnoprstxyz
I try this:
var fs = require('fs');
var path = require('path');
var fd = fs.openSync(path.join(process.cwd(), 'test.txt'), 'a+');
var buf=Buffer.from("++","utf8");
fs.writeSync(fd, buf, 0,2, 10);
console.log('closing file now')
fs.closeSync(fd);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question