0

I want to append 4 bytes to a raw data file.

This code would write text, but there's no guide to doing raw bytes

fs = require('fs');
fs.writeFile('helloworld.txt', 'Hello World!', function (err) {
  if (err) return console.log(err);
  console.log('Hello World > helloworld.txt');
});
Lolums
  • 902
  • 3
  • 9
  • 34

1 Answers1

0

fs.writeFile() accepts a Buffer as well as a string.

This example would append three 0x01 bytes to a file:

const fs = require('fs');

fs.writeFile('helloworld.txt', Buffer.from([0x01, 0x01, 0x01]), { flag: "a" }, function (err) {
  if (err) return console.log(err);
  console.log('Hello World > helloworld.txt');
});
Caleb Denio
  • 1,054
  • 8
  • 14