Append to a text file using Bonescript

Hello all.
This is probably a dumb question but, is there a way to write to a text file using Bonescript without erasing the current contents of the file? I am using writeTextFile, but the file content gets replaced by the last data sent there.
I am trying to output the readings of a sensor to an ASCII text file every set interval of time. I am very new to BeagleBone, and my application is quite simple, so I am trying to do this the easiest way possible.

Not sure if there is a better way, but you could do a readTextFile, store the data in a string, append your data to the string, then writeTextFile the data to a file. This would work well until your text files start getting large, after which things will start getting slow and memory-intensive.

function appendTextFile(filename,text){
var b = require(‘bonescript’);
b.readTextFile(filename, printStatus);
function printStatus(x) {
if (x.err===null) + text;
else textout = text;

// and if you want time tags, e.g. for an html log file
function appendTagged(filename,text){
\n’+Date().substr(4,21) + text)

I think nodejs’s appendFile[1] will do just what you want.



Hello friend. I think maybe is too late for you, but if someone is alredy looking for information about that, i took the solution.
In the bonescript library i didn’t find a function to append in a text file, so i used the function appendFile(), from node.js library. To use this function, we have to write var fs = require(‘fs’);

In my code, i read the value of a pin and i write this value on a text file, append to the other values. This is my code:

var b = require(‘bonescript’);
var fs = require(‘fs’);

b.pinMode(‘P9_11’, b.INPUT);

setInterval(copyInputToFile, 1000);

function copyInputToFile() {
b.digitalRead(‘P9_11’, writeToFile);
function writeToFile(x) {
fs.appendFile(‘datos.txt’, x.value);

I hope you find it useful.

Pedro Salcedo