Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <float.h>
- #define DATA_F "dati.txt"
- #define FILE_ORD "ordered.txt"
- #define STRLEN 256
- typedef unsigned int uint;
- // funzioni di bellezza
- unsigned int my_strlen(char *str) //returns string lenght
- { unsigned int i = 0; while(str[i] != '\0') i++; return i; }
- //::::
- int main(void)
- {
- uint nf, nv; float threshold, mean; uint* pv; uint i,j;
- FILE* pf; FILE** ppfiles; char str[STRLEN], *label;
- if((pf=fopen(DATA_F,"r"))==NULL) goto f_err; fscanf(pf,"%s\n",str);
- if((label=(char*)malloc(sizeof(char)*my_strlen(str)))==NULL) goto m_err;
- sprintf(label,"%s",str); //name of files is placed in a string of fitting lenght...
- fscanf(pf,"%s\n",str); nf=atoi(str);
- fscanf(pf,"%s\n",str); nv=atoi(str);
- fscanf(pf,"%s",str); threshold=atof(str); fclose(pf);
- printf("Reading from '%s' nf=%u, nv=%u, threshold=%f\n",DATA_F,nf,nv,threshold);
- if( (pv=(uint*)malloc(sizeof(uint)*nf))==NULL) goto m_err;
- if( (pf=fopen(FILE_ORD,"w")) == NULL) { free(pv); goto f_err; }
- if( (ppfiles = (FILE**)malloc(sizeof(FILE*)*nf)) == NULL) { fclose(pf); free(pv); goto m_err; }
- for(i=0; i<nf; i++) //opening files containing data
- {
- sprintf(str,"%s%u.txt",label,i);
- if((ppfiles[i]=fopen(str,"r"))==NULL)
- { for(i--;i>=0;i--) fclose(ppfiles[i]); fclose(pf); free(pv); goto f_err; }
- }
- for(i=0; i<nv; i++) //navigating through number of ROWs
- {
- for(mean=j=0; j<nf; j++) { fscanf(ppfiles[j],"%s\n",str); mean+=pv[j]=atoi(str); }
- printf("mean[%u]=%f -> ",i, mean/=nf);
- if(mean>threshold) { printf("REJECTED\n"); continue; }
- printf("ACCEPTED\n"); // & printing in txt file...
- for(j=nf;j>0;) fprintf(pf,"%u\t",pv[--j]); fprintf(pf,"\n");
- }
- for(i=0; i<nf; i++) fclose(ppfiles[i]); fclose(pf); free(pv);
- printf("\nEOP\n"); return EXIT_SUCCESS;
- m_err: printf("Memory error"); return EXIT_FAILURE;
- f_err: printf("File error"); return EXIT_FAILURE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement