Aurox_

database_mail.c

Dec 14th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define FILEIN    "./filetxt/mail.txt"
  5. #define FILEOUT   "./filetxt/mail"
  6. #define START     'D'
  7. #define END       'E'
  8. #define STOP      'S'
  9. #define STRLEN    1024
  10. #define L_MIN     1
  11. #define L_MAX     10
  12.  
  13. typedef FILE*   pfile;
  14. typedef char*   pchar;
  15. typedef float*  pfloat;
  16.  
  17. int stringa_contiene_char(pchar ps, char carattere)
  18.   {
  19.     while (*ps!='\0')
  20.       {
  21.         if (*ps==carattere) return 1;
  22.         ps++;
  23.       }
  24.     return 0;
  25.   }
  26.  
  27. int inputRequest(pchar question, pchar pinput, unsigned strlen)
  28.   {
  29.     unsigned len;
  30.     printf("%s: ",question);
  31.     return ((len=scanf("%s",pinput))<strlen) ? len:-1;
  32.   }
  33.  
  34. int save_buffer (pchar nome, unsigned n, pfloat buffer, unsigned nval)
  35.   {
  36.     char s[STRLEN];
  37.     pfloat p,pmax;
  38.     pfile pfw;
  39.  
  40.     sprintf(s,"%s.%u.txt",nome,n);
  41.     if ((pfw=fopen(s,"w"))==NULL) return 1;
  42.     fprintf(pfw,"%u\n",nval);
  43.  
  44.     pmax=(p=buffer)+nval;
  45.     while (p<pmax)
  46.       {
  47.         fprintf(pfw,"%f\n",(*p));
  48.         p++;
  49.       }
  50.     fclose(pfw);
  51.     return 0;
  52.   }
  53.  
  54. int main(void)
  55.   {
  56.     int lb;
  57.     unsigned check,nr,ns,nd,i,n;
  58.     char s[STRLEN],stemp[STRLEN];
  59.     pfloat buffer;
  60.     pfile pfr;
  61.  
  62.     check=1;
  63.     do
  64.       {
  65.         sprintf(stemp,"Qual e' il valore di lb? [%d,%d]",L_MIN,L_MAX);
  66.         inputRequest(stemp,s,STRLEN);
  67.         lb=atoi(s);
  68.         if ((lb<L_MIN)||(lb>L_MAX)) printf("Inserisci un numero compreso tra %d e %d\n",L_MIN,L_MAX);
  69.         else
  70.           {
  71.             printf("Ho ricevuto %d.\n",lb);
  72.             check=0;
  73.           }
  74.       }
  75.     while (check);
  76.  
  77.     if ((buffer=(pfloat)malloc(sizeof(float)*lb))==NULL) goto err_mem;
  78.     if ((pfr=fopen(FILEIN,"r"))==NULL) goto err_file_r;
  79.  
  80.     ns=nd=nr=i=n=0;
  81.     do /*in questo caso meglio usare il break*/
  82.       {
  83.         fscanf(pfr,"%s",s);
  84.         nr++;
  85.         if (stringa_contiene_char(s,START))
  86.           {
  87.             ns++;
  88.             check=1;
  89.             while (check)
  90.               {
  91.                 fscanf(pfr,"%s",s);
  92.                 nr++;
  93.                 if (!stringa_contiene_char(s,END))
  94.                   {
  95.                     buffer[i]=atof(s);
  96.                     nd++;
  97.                     printf("nr=%u, ns=%u, nd=%u -> Letto: %.2f\n",nr,ns,nd,buffer[i]);
  98.                     i++;
  99.                     if (i>=lb)
  100.                       {
  101.                         save_buffer(FILEOUT,n,buffer,i);
  102.                         printf("Scrivo il file: %s.%u.txt\n",FILEOUT,n);
  103.                         n++;
  104.                         i=0;
  105.                       }
  106.                   }
  107.                 else check=0;
  108.               }
  109.           }
  110.       }
  111.     while (!stringa_contiene_char(s,STOP));
  112.     if (i)
  113.       {
  114.         save_buffer(FILEOUT,n,buffer,i);
  115.         printf("Scrivo il file: %s.%u.txt\n",FILEOUT,n);
  116.       }
  117.  
  118.     fclose(pfr);
  119.    
  120.     return 0;
  121.     err_file_r:
  122.       printf("Errore lettura file\n"); return EXIT_FAILURE;
  123.     err_mem:
  124.       printf("Errore memoria\n"); return EXIT_FAILURE;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment