Advertisement
gigba

copia_recor_in_file

Dec 16th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. /* E' necessario che venga creato un file di nome input.txt dove ci sono i voti
  2.  intervallati da uno spazio "Quanti ne vuoi รจ del tutto arbitrario".
  3.  
  4.  E' necessario l'esecuzione avvenga da line di comando come segue:
  5.  "nome-programma.exe", input.txt, output.txt
  6.  
  7.  E' necessario che il file venga compilato da linea di comando come segue:
  8.  gcc nome-programma.c -o nome-programma.exe
  9.  */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. typedef struct studente{
  15.     int  matricola;
  16.     char cognome[20];
  17.     char nome[20];
  18.     char luogo[20];
  19.     int  anno;
  20.     int  *esami;
  21. }studente;
  22.  
  23. /*PROTOTIPI*/
  24.  
  25. void controlla_input(int);
  26. void controlla_file(FILE *,char *);
  27. void conta_in_file_esami(FILE*,int*);
  28. void update(char*[],studente *,FILE *);
  29. void scrivi_file(FILE *,studente,int);
  30.  
  31. int main(int argc, char *argv[]){
  32. char *info[]={"Verde","Luisa","Napoli"};
  33. studente rec;
  34. int num_esami = 0;
  35. FILE *input, *output;
  36.  
  37.     controlla_input(argc);
  38.     input = fopen(argv[1],"r");
  39.     controlla_file(input,argv[1]);
  40.     output = fopen(argv[2],"w");
  41.     controlla_file(output,argv[2]);
  42.     conta_in_file_esami(input,&num_esami);
  43.     rec.esami = calloc(num_esami,sizeof(int));
  44.     update(info,  &rec, input);
  45.     scrivi_file(output,rec,num_esami);
  46.     return 0;
  47.  
  48. }
  49.  
  50.  
  51.  
  52. void controlla_input(int num){
  53.     if(num!=3){
  54.     printf("Likely there is a mismatch in how the program command had to be called\n");
  55.     printf("The right format is : 'Command Name', 'Argument 1', Argument 2");
  56.     exit(EXIT_FAILURE);
  57.     }
  58.     printf("Control input done and is ok.\n");
  59. }
  60. void controlla_file(FILE *f,char *file_name){
  61. if(f==NULL){
  62.     printf("Sorry there are some issue.\n");
  63.     printf("The more likely reasons are :\n");
  64.     printf("Maybe are you typed bad a name? Check the name: %s\n", file_name);
  65.     printf("Maybe haven't you a right permission on file?\n");
  66.     printf("In last, likely be the system downfall:\n");
  67. }
  68.     printf("Congrats, the file %s is opened successfully\n",file_name);
  69. }
  70.  
  71.  
  72. void conta_in_file_esami(FILE *f,int *counter){
  73.     int voto;
  74.     while(fscanf(f,"%d",&voto)!=EOF){
  75.         (*counter)++;
  76.     }
  77. }
  78.  
  79. void update(char *dati_parziali[], studente *stud, FILE *f){
  80.     int i=0;
  81.     strcpy(stud->cognome,dati_parziali[0]);
  82.     strcpy(stud->nome,dati_parziali[1]);
  83.     strcpy(stud->luogo,dati_parziali[2]);
  84.     stud->matricola=5128599;
  85.     stud->anno=2017;
  86.     rewind(f);// Serve a portare il puntatore all'inizio file
  87.              // l'ultima scanf lo aveva portato a fine file
  88.              // e sarebbe rimasto li senza il rewinfd perche
  89.              // i file non sono stati ancora chiusi.
  90.     while(fscanf(f,"%d",&stud->esami[i]) != EOF){
  91.         i++;
  92.     }
  93. }
  94.  
  95. void scrivi_file(FILE *f,studente stud,int size){
  96.     int i;
  97.     fprintf(f, "THE STUDENT...\nidentification number: %d\n"
  98.                "Surname: %s\nName. %s\nCity: %s\nYear: %d\n"
  99.                "he has obtained the following grades:\n",
  100.                                          stud.matricola,
  101.                                          stud.cognome,
  102.                                          stud.nome,
  103.                                          stud.luogo,
  104.                                          stud.anno);
  105.  
  106.     for(i=0;i<size;i++){
  107.        fprintf(f,"%d ",stud.esami[i]);
  108.  
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement