Page 1 of 1

How to write only latest values with DSVWriter?

Posted: Sun 06 Jan 2013, 18:25
by pekkak
I need a text file of e.g. 100 latest temperature values to draw a graph. If I use DSVWriter plugin with setting "append=true", the file keeps growing forever. Is there anyway to make it delete the first line when it adds a new line in the end when a specified number of rows have been reached?

Or can I somehow (preferably with PHP) query this data from Switch King DB?

Re: How to write only latest values with DSVWriter?

Posted: Sun 06 Jan 2013, 20:39
by pekkak
Before a more sofisticated solution I will clean up the beginning of the file leaving 40 rows in the end and create headers with PHP like this:

$csvfile = "ulko.csv";
$keep = 40;
$n = count(file($csvfile));
if ($n > $keep) {
$lines = file($csvfile);
$first_line = $lines[0];
$lines = array_slice($lines, $n - $keep);
$lines = array_merge(array("Temperature;Time\n"), $lines);
$file = fopen($csvfile, 'w');
fwrite($file, implode('', $lines));
fclose($file);
}