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');
});
Lucien
  • 776
  • 3
  • 12
  • 40

1 Answers1

-1

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,465
  • 8
  • 15