0

I need to write a sequence of boolean and float values to a file in binary using Javascript. I would like to keep the file open and then just do a sequence of writes. Here is the pseudocode of what I would like to accomplish

file = open('filename.dat', 'ab') // open in "append binary" mode
file.write(true); // write 1 bit to file
file.write(3.14) // write 64 bit float number to file
file.close()

Does anyone know how to do this in Javascript? Thanks a lot.

  • 1
    Create the binary data you want in the file in a Buffer object and then write the Buffer to the file. If you want to keep the file open, just don't call `.close()`. – jfriend00 Feb 07 '23 at 07:51
  • 1
    Note [Buffer methods](https://nodejs.org/api/buffer.html) such as `.writeDoubleLE()` and `.writeUint8()` that let you write a Javascript number and an unsigned 8-bit value to a buffer. – jfriend00 Feb 07 '23 at 07:58
  • Thanks it worked. The trick was to use a createWriteStream and a buffer. – Alexander Richter Feb 07 '23 at 08:05

0 Answers0