Append to a text file using Bonescript

179 views
Skip to first unread message

ortegaan...@gmail.com

unread,
Oct 20, 2014, 2:09:23 PM10/20/14
to beagl...@googlegroups.com
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.
Thanks.

Michael M

unread,
Oct 20, 2014, 2:47:38 PM10/20/14
to beagl...@googlegroups.com
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.

mced...@gmail.com

unread,
Mar 2, 2016, 4:26:19 PM3/2/16
to BeagleBoard, ortegaan...@gmail.com

function appendTextFile(filename,text){
var b = require('bonescript');
    b.readTextFile(filename, printStatus);
function printStatus(x) {
    if (x.err===null) textout=x.data + text;
    else textout = text;
    b.writeTextFile(filename,textout);
}
}
 
// and if you want time tags, e.g. for an html log file
function appendTagged(filename,text){
    appendTextFile(filename,'<br>\n'+Date().substr(4,21) + text)
}
 

Mark A. Yoder

unread,
Mar 3, 2016, 10:15:37 AM3/3/16
to BeagleBoard, ortegaan...@gmail.com, mced...@gmail.com
I think nodejs's appendFile[1] will do just what you want.

friker...@gmail.com

unread,
Sep 24, 2018, 3:22:47 PM9/24/18
to BeagleBoard
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) {
         b.echo(x.value);
         fs.appendFile('datos.txt', x.value);
    }
}

I hope you find it useful. 

Pedro Salcedo
Reply all
Reply to author
Forward
0 new messages