/* C includes */
#include <stdio.h>
#include <stdlib.h>


/* local includes */
#include "stmc.h"


/* *** parameters *** */
stmc_int Ndf=3; /* number of the data files */


int main(void)
{
  stmc_char file_name[80];
  stmc_int i,j,N;
  stmc_real08 x,*data;
  FILE *fp;
  
  /* loop on different data files */
  for(i=0;i<Ndf;i++)
    {
      /* create file name */
      sprintf(file_name,"dat%1d.d",(i+1));
         /* files numbering is 1-based */
      printf("Processing data file: %s\n",file_name);
      
      /* first pass reading to calculate number of data points */
      fp=fopen(file_name,"rt");
      if(NULL!=fp) /* open the file successfully */
        {
          j=0;
          while(1==fscanf(fp,"%lf\n",&x))
            {
              j++;
            }
          fclose(fp);
          N=j;
          
          /* create array for storing data */
          data=(stmc_real08*)malloc((size_t)N*sizeof(stmc_real08));
          
          fp=fopen(file_name,"rt");
          /* second pass reading - actually store the data */
          for(j=0;j<N;j++)
             fscanf(fp,"%lf\n",&(data[j]));
          fclose(fp);

          /* output info about the data */          
          printf("  N = %d    data[N-1] = %f\n",N,data[N-1]);
          
          
          /* *** DO AVERAGE, ERROR BARS, ETC. HERE *** */
          
          
          /* free data array */
          free(data);
        }
      else /* failure to open the file */
        {
          printf("File %s cannot be read\n\n",file_name);
        }
      
    }
  
  return 0;

}
      
/* include all used routines */
#ifndef STMC_C_COMPILED_AS_LIBRARY
#include "stmc_steb0.c"
#endif
