Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. typedef struct data{
  7.  
  8. char nome[20];
  9. char cognome[20];
  10. char cf[20];
  11.  
  12. }data;
  13.  
  14. void controllo_input(int);
  15. void controllo_file(FILE*);
  16. void leggi_file(FILE*, data**, int*);
  17. void aggiorna(data*, char*, char*, char*, int);
  18. void scrivi_file(FILE*, data*, int);
  19.  
  20.  
  21. int main(int argc,char *argv[]){
  22.  
  23. FILE *one, *two;
  24. data *array;
  25. int dim;
  26. char nome[20], cognome[20], cf[20];
  27.  
  28. controllo_input(argc);
  29. one=fopen(argv[1],"r");
  30. controllo_file(one);
  31. two=fopen(argv[2],"w");
  32. controllo_file(two);
  33.  
  34. leggi_file(one, &array, &dim);
  35.  
  36. printf("\n Inserire nome - cognome - cf: ");
  37. scanf("%s%s%s", nome, cognome, cf);
  38.  
  39. aggiorna(array, nome, cognome, cf, dim);
  40.  
  41. scrivi_file(two, array, dim);
  42.  
  43. printf("\n");
  44. fclose(one);
  45. fclose(two);
  46. return 0;
  47. }
  48.  
  49. void controllo_input(int argc){
  50.  
  51. if(argc!=3){
  52. fprintf(stdout, "\n errore, mancanza di file. \n");
  53. exit(EXIT_FAILURE);
  54. } }
  55.  
  56. void controllo_file(FILE *file){
  57.  
  58. if(file==NULL){
  59. fprintf(stdout, "\n errore apertura file. \n");
  60. exit(EXIT_FAILURE);
  61. } }
  62.  
  63. void leggi_file(FILE *input, data **array, int *dim){
  64.  
  65. data in;
  66.  
  67. while(fscanf(input, "%s%s%s", in.nome, in.cognome, in.cf)!=EOF){
  68. (*dim)++;
  69. }
  70.  
  71. *array=calloc(*(dim+1), sizeof(data));
  72.  
  73. for(int i=o; i<*(dim+1); i++){
  74. fscanf(input, "%s%s%s", array[0][i].nome, array[0][i].cognome, array[0][i].cf);
  75. } }
  76.  
  77.  
  78. void aggiorna(data *vect, char *nome, char *cognome, char *cf, int dim){
  79.  
  80. strcpy(vect[dim].nome, nome);
  81. strcpy(vect[dim].cognome, cognome);
  82. strcpy(vect[dim].cf, cf);
  83. }
  84.  
  85. void scrivi_file(FILE *output, data *vect, int dim){
  86.  
  87. fprintf(output, "%s %s %s", vect[dim].nome, vect[dim].cognome, vect[dim].cf);
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement