Advertisement
titant3k

12febbraio V2

Feb 4th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <float.h>
  5.  
  6. #define DATA_F "dati.txt"
  7. #define FILE_ORD "ordered.txt"
  8.  
  9. #define STRLEN 256
  10.  
  11. typedef unsigned int uint;
  12.  
  13. // funzioni di bellezza
  14. unsigned int my_strlen(char *str)     //returns string lenght
  15.  {  unsigned int i = 0;   while(str[i] != '\0') i++;   return i;  }
  16. //::::
  17.  
  18. int main(void)
  19.  {
  20.   uint nf, nv;     float threshold, mean;    uint* pv;     uint i,j;
  21.   FILE* pf;  FILE** ppfiles;    char str[STRLEN], *label;      
  22.  
  23.   if((pf=fopen(DATA_F,"r"))==NULL) goto f_err;       fscanf(pf,"%s\n",str);
  24.  
  25.   if((label=(char*)malloc(sizeof(char)*my_strlen(str)))==NULL) goto m_err;
  26.   sprintf(label,"%s",str);  //name of files is placed in a string of fitting lenght...
  27.  
  28.   fscanf(pf,"%s\n",str);    nf=atoi(str);
  29.   fscanf(pf,"%s\n",str);    nv=atoi(str);
  30.   fscanf(pf,"%s",str);      threshold=atof(str);    fclose(pf);
  31.   printf("Reading from '%s' nf=%u, nv=%u, threshold=%f\n",DATA_F,nf,nv,threshold);
  32.  
  33.   if( (pv=(uint*)malloc(sizeof(uint)*nf))==NULL) goto m_err;
  34.   if( (pf=fopen(FILE_ORD,"w")) == NULL) { free(pv);  goto f_err; }
  35.   if( (ppfiles = (FILE**)malloc(sizeof(FILE*)*nf)) == NULL) { fclose(pf); free(pv); goto m_err; }
  36.   for(i=0; i<nf; i++)   //opening files containing data
  37.    {
  38.      sprintf(str,"%s%u.txt",label,i);  
  39.      if((ppfiles[i]=fopen(str,"r"))==NULL)
  40.       { for(i--;i>=0;i--) fclose(ppfiles[i]);   fclose(pf); free(pv);  goto f_err;  }
  41.    }
  42.  
  43.   for(i=0; i<nv; i++) //navigating through number of ROWs
  44.    {
  45.     for(mean=j=0; j<nf; j++) { fscanf(ppfiles[j],"%s\n",str);  mean+=pv[j]=atoi(str); }
  46.     printf("mean[%u]=%f -> ",i, mean/=nf);
  47.     if(mean>threshold) { printf("REJECTED\n"); continue; }
  48.     printf("ACCEPTED\n");   // & printing in txt file...
  49.     for(j=nf;j>0;) fprintf(pf,"%u\t",pv[--j]);     fprintf(pf,"\n");
  50.    }
  51.    
  52.   for(i=0; i<nf; i++) fclose(ppfiles[i]);      fclose(pf);     free(pv);
  53.  
  54.   printf("\nEOP\n");   return EXIT_SUCCESS;
  55.   m_err: printf("Memory error"); return EXIT_FAILURE;
  56.   f_err: printf("File error");   return EXIT_FAILURE;
  57.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement