Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define FILEIN "./filetxt/mail.txt"
- #define FILEOUT "./filetxt/mail"
- #define START 'D'
- #define END 'E'
- #define STOP 'S'
- #define STRLEN 1024
- #define L_MIN 1
- #define L_MAX 10
- typedef FILE* pfile;
- typedef char* pchar;
- typedef float* pfloat;
- int stringa_contiene_char(pchar ps, char carattere)
- {
- while (*ps!='\0')
- {
- if (*ps==carattere) return 1;
- ps++;
- }
- return 0;
- }
- int inputRequest(pchar question, pchar pinput, unsigned strlen)
- {
- unsigned len;
- printf("%s: ",question);
- return ((len=scanf("%s",pinput))<strlen) ? len:-1;
- }
- int save_buffer (pchar nome, unsigned n, pfloat buffer, unsigned nval)
- {
- char s[STRLEN];
- pfloat p,pmax;
- pfile pfw;
- sprintf(s,"%s.%u.txt",nome,n);
- if ((pfw=fopen(s,"w"))==NULL) return 1;
- fprintf(pfw,"%u\n",nval);
- pmax=(p=buffer)+nval;
- while (p<pmax)
- {
- fprintf(pfw,"%f\n",(*p));
- p++;
- }
- fclose(pfw);
- return 0;
- }
- int main(void)
- {
- int lb;
- unsigned check,nr,ns,nd,i,n;
- char s[STRLEN],stemp[STRLEN];
- pfloat buffer;
- pfile pfr;
- check=1;
- do
- {
- sprintf(stemp,"Qual e' il valore di lb? [%d,%d]",L_MIN,L_MAX);
- inputRequest(stemp,s,STRLEN);
- lb=atoi(s);
- if ((lb<L_MIN)||(lb>L_MAX)) printf("Inserisci un numero compreso tra %d e %d\n",L_MIN,L_MAX);
- else
- {
- printf("Ho ricevuto %d.\n",lb);
- check=0;
- }
- }
- while (check);
- if ((buffer=(pfloat)malloc(sizeof(float)*lb))==NULL) goto err_mem;
- if ((pfr=fopen(FILEIN,"r"))==NULL) goto err_file_r;
- ns=nd=nr=i=n=0;
- do /*in questo caso meglio usare il break*/
- {
- fscanf(pfr,"%s",s);
- nr++;
- if (stringa_contiene_char(s,START))
- {
- ns++;
- check=1;
- while (check)
- {
- fscanf(pfr,"%s",s);
- nr++;
- if (!stringa_contiene_char(s,END))
- {
- buffer[i]=atof(s);
- nd++;
- printf("nr=%u, ns=%u, nd=%u -> Letto: %.2f\n",nr,ns,nd,buffer[i]);
- i++;
- if (i>=lb)
- {
- save_buffer(FILEOUT,n,buffer,i);
- printf("Scrivo il file: %s.%u.txt\n",FILEOUT,n);
- n++;
- i=0;
- }
- }
- else check=0;
- }
- }
- }
- while (!stringa_contiene_char(s,STOP));
- if (i)
- {
- save_buffer(FILEOUT,n,buffer,i);
- printf("Scrivo il file: %s.%u.txt\n",FILEOUT,n);
- }
- fclose(pfr);
- return 0;
- err_file_r:
- printf("Errore lettura file\n"); return EXIT_FAILURE;
- err_mem:
- printf("Errore memoria\n"); return EXIT_FAILURE;
- }
Advertisement
Add Comment
Please, Sign In to add comment