Mercurial > hg > fxanalyse
diff FXAnalyse.c @ 183:791ca26fee6a
Rewrite data writing to file
Fmt() uses by default truncation to conver double arguments to their text
representation. Rounding must be used. Rewrite using standard C functions to
get rid of this problem (and probably make it more efficient). Extend to handle
arbitrary number of channels and to report errors on opening the data files.
author | Daniele Nicolodi <daniele.nicolodi@obspm.fr> |
---|---|
date | Fri, 21 Feb 2014 18:42:30 +0100 |
parents | 8faada7e4faf |
children | 12df3a2b18de |
line wrap: on
line diff
--- a/FXAnalyse.c Fri Feb 21 18:42:29 2014 +0100 +++ b/FXAnalyse.c Fri Feb 21 18:42:30 2014 +0100 @@ -1,6 +1,7 @@ #include <tcpsupp.h> #include <utility.h> #include <ansi_c.h> +#include <lowlvlio.h> #include <cvirte.h> #include <userint.h> #include <formatio.h> @@ -300,28 +301,34 @@ static void write_data(const char *folder, const char *name, const char *id, const char *timestr, double utc, double *v, int nchan) { + int i, fd, len; char line[1024]; char filename[FILENAME_MAX]; // construct filename in the form folder\\id-name.txt snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name); - int fd = OpenFile(filename, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII); - switch (nchan) - { - case 1: - Fmt(line, "%s\t%f[p3]\t%f[p3]", - timestr, utc, v[0]); - break; - case 4: - Fmt(line, "%s\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]", - timestr, utc, v[0], v[1], v[2], v[3]); - break; - default: - strcpy(line, "?"); + fd = open(filename, O_CREAT|O_WRONLY|O_APPEND); + if (fd < 0) { + logmessage(ERROR, "open data file %s: %s", filename, strerror(errno)); + return; } - WriteLine(fd, line, -1); - CloseFile(fd); + + // timestamp + len = snprintf(line, sizeof(line), "%s\t%.3f", timestr, utc); + + // data channels + for (i = 0; i < nchan; i++) + len += snprintf(line + len, sizeof(line) - len, "\t%.3f", v[i]); + + // newline + line[len++] = '\r'; + line[len++] = '\n'; + + // write to file + write(fd, line, len); + + close(fd); }