# HG changeset patch # User Daniele Nicolodi # Date 1393004550 -3600 # Node ID 791ca26fee6aae6c17a160761b616f8c4503ac08 # Parent 4d1f9e222e9e55e237b35fbd5655d5f5959b2b37 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. diff -r 4d1f9e222e9e -r 791ca26fee6a FXAnalyse.c --- 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 #include #include +#include #include #include #include @@ -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); }