I have a sample array as follows
var arr = [ [ 1373628934214, 3 ], [ 1373628934218, 3 ], [ 1373628934220, 1 ], [ 1373628934230, 1 ], [ 1373628934234, 0 ], [ 1373628934237, -1 ], [ 1373628934242, 0 ], [ 1373628934246, -1 ], [ 1373628934251, 0 ], [ 1373628934266, 11 ] ]
I would like to write this array to a file such as I get a file as follows
1373628934214, 3 1373628934218, 3 1373628934220, 1 ...... ......
Since the size of the data is fixed, one simple way of writing this entire array into a file is using the binary writing mode: FILE *f = fopen("client. data", "wb"); fwrite(clientdata, sizeof(char), sizeof(clientdata), f); fclose(f);
You can define your array on the exports object in order to allow to import it. You could create a . json file as well, if your array only contains simple objects. If you require a module (for the first time), its code is executed and the exports object is returned and cached.
If it's a huuge array and it would take too much memory to serialize it to a string before writing, you can use streams:
var fs = require('fs'); var file = fs.createWriteStream('array.txt'); file.on('error', function(err) { /* error handling */ }); arr.forEach(function(v) { file.write(v.join(', ') + '\n'); }); file.end();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With