home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / fluke13.zip / MAKECSV.C < prev    next >
C/C++ Source or Header  |  1995-07-03  |  3KB  |  108 lines

  1. /*
  2. makecsv <filename>
  3.  
  4. This program looks at 2 input files and creates a third file.
  5. The user provides the basename of the files (8 character maximum,
  6. no extension.
  7.  
  8. The program then looks for:
  9. <filename>.dat which should be 512 bytes long, binary data from meter
  10. <filename>.hdr which contains an ASCII text header
  11.  
  12. and creates
  13. <filename>.csv which is a a 512 line file with the x value,yvalue
  14. for each data point on a separate line.
  15.  
  16. Program exits with 1 if an error occured, 0 if ok.
  17. */
  18.  
  19. /* external program to process binary fluke data to csv files */
  20. /* V1.1, fixed problem with reading a null data file */
  21. /* V1.2, added additional documentation, changed error returns for exit() */
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26.  
  27. main (int argc, char **argv, char**envp)
  28. {
  29.     /* used for building up strings */
  30.     char tmpstr[80];
  31.  
  32.     /* the .hdr values are read into here */
  33.     char id[80],yunit[80],xunit[80];
  34.     double yzero,xzero,ydelta,xdelta;
  35.     int ylevel,xsample;
  36.  
  37.     /* data is put here */
  38.     unsigned char values[512];
  39.     int counter;
  40.  
  41.     /* calculated values for csv file */
  42.     double realx,realy;
  43.  
  44.     /* file pointer */
  45.     FILE *infile;
  46.  
  47.     int reterror;
  48.  
  49.     /* build up the name of the .hdr file */
  50.     sprintf(tmpstr, "%s.hdr", argv[1]);
  51.     if ((infile=fopen(tmpstr, "rb")) == NULL)
  52.     {
  53.         printf("ERROR: can't find file %s\n", tmpstr);
  54.         exit(1);
  55.     }
  56.  
  57.     /* please note that this file IS NOT free format */
  58.     /* in fact, the numeric quantities must come first since this */
  59.     /* routine depends on the 'terminate at whitespace' property */
  60.     /* of atof and atoi */
  61.     fgets(id,80,infile);
  62.     fgets(xunit,80,infile);
  63.     fgets(yunit,80,infile);
  64.     fgets(tmpstr,80,infile);
  65.     xzero=atof(tmpstr);
  66.     fgets(tmpstr,80,infile);
  67.     yzero=atof(tmpstr);
  68.     fgets(tmpstr,80,infile);
  69.     xdelta=atof(tmpstr);
  70.     fgets(tmpstr,80,infile);
  71.     ydelta=atof(tmpstr);
  72.     fgets(tmpstr,80,infile);
  73.     xsample=atoi(tmpstr);
  74.     fgets(tmpstr,80,infile);
  75.     ylevel=atoi(tmpstr);
  76.     fclose(infile);
  77.  
  78.     /* get the data file */
  79.     sprintf(tmpstr, "%s.dat", argv[1]);
  80.     if ((infile=fopen(tmpstr, "rb")) == NULL)
  81.     {
  82.         printf("ERROR: can't find file %s\n", tmpstr);
  83.         exit(1);
  84.     }
  85.     if (512 != fread((void *)values, sizeof(char), 512, infile))
  86.     {
  87.         printf("ERROR: improper data format in %s\n", tmpstr);
  88.         exit(1);
  89.     }
  90.     fclose(infile);
  91.  
  92.     /* write it out */
  93.     sprintf(tmpstr, "%s.csv", argv[1]);
  94.     if ((infile=fopen(tmpstr, "wb")) == NULL)
  95.     {
  96.         printf("ERROR: can't write file %s\n", tmpstr);
  97.         exit(1);
  98.     }
  99.     for(counter=0;counter<512;counter++)
  100.     {
  101.         realx = ((float)counter * xdelta) + xzero;
  102.         realy = (((float)values[counter] - 128.0) * ydelta) - yzero;
  103.         fprintf (infile,"%.3e,%.3e\n",realx,realy);
  104.     }
  105.     fclose(infile);
  106.     exit(0);
  107. }
  108.