0

I want to append to second last line of a file using NodeJs!

I hope I am making sense.

Thanks in advance

My current code which appends to end of the file:

fs.appendFile('data.txt', 'something', function (err) {
  if (err) throw err;
  console.log('Saved!');
});
Prog 14936
  • 15
  • 4
  • There's a lot to cover here, please may you show us what you've already got using a [mcve]? We can't just give you a solution. – evolutionxbox May 14 '22 at 12:21
  • Ofcourse, I have added my current working code which appends text to the end of the file @evolutionxbox – Prog 14936 May 14 '22 at 12:25
  • Does this answer your question? [Write a line into a .txt file with Node.js](https://stackoverflow.com/questions/33418777/write-a-line-into-a-txt-file-with-node-js) – evolutionxbox May 14 '22 at 12:27
  • if your file is not too huge, you can read it in ram with fs.readFile. then you can loop from the end of the resulting buffer to find \n. then insert data at this position. then save the file on the disk. – Raphael PICCOLO May 14 '22 at 12:29

0 Answers0