Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with FileSystem and storing data as binary

Hi,

I'm hoping someone can help me figure out how to correctly read and write buffer data...

 

I have a clockface I'm working on that stores activity related data at 15 minute intervals. Currently I'm storing the data like so:

 

 

let data = {};
...
// Writing
data["point1"] = {"x1": 123, "y1": 123, "x2": 123, y2: "123", "opacity": 0.3};
fs.writeFileSync("file.txt", data, "cbor");
...
// Reading
fs.readFileSync("file.txt", "cbor");

 

 


This has been working but eventually the clockface freezes, I think due to memory issues from reading the file once a lot of keys have been saved.

 

I've read a few similar questions, where it's recommended this data is stored in binary. I'm trying to make these changes but I'm having issues writing the file. Here's what I'm trying to do:

 

 

data = [123, 123, 123, 132, 0.3]
let buffer = new Float64Array(data);
fs.writeFileSync("file.txt", buffer);
...
fs.readFileSync("file.txt", "utf-8");

 

 

Reading the file returns an empty string.

 

So my questions are:

1. What am I doing wrong above?

2. How can I store the point name as well, so data is ["point1", 123, 123, 123, 123, 0.3]
3. Is it possible to use writeFileSync instead of writeSync for these buffers? I'd like to write the entire file at once, to avoid dupes. This data can be saved at any point, like if the display turns on. So I'll need to update the data for that point if it already exists.

 

Thanks in advance!

Best Answer
0 Votes
2 REPLIES 2

utf-8 is a character encoding, whereas your data is binary.

Arrays can only hold data of one type, so you can't put a string in it. Can you use numbers to identify points?

If the file gets created, you should be able to find it among the sim data files on your computer. That can be helpful to verify that the contents is what you'd expect, and to establish whether the problem is reading or writing.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Hey Gondwana,

 

Thanks for the reply.

 

I knew utf-8 was the wrong encoding, I thought an encoding was required as per the docs - but now I see that's not true.

That's helpful re: arrays can only hold one data type, thanks for that. I'll have to figure out a way to represent the points numerically.

 

I'm able to verify the file is created in the simulator files, but it's empty. Any idea what I have wrong in my sample code for writing to the file? Thanks for your help.

 

Jamie

Best Answer
0 Votes