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?
How to write only latest values with DSVWriter?
-
- 2 stars
- Posts: 44
- Joined: Sat 10 Nov 2012, 07:31
-
- 2 stars
- Posts: 44
- Joined: Sat 10 Nov 2012, 07:31
Re: How to write only latest values with DSVWriter?
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);
}
$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);
}
Who is online
Users browsing this forum: No registered users and 5 guests