Why does this code only writes the first and the last occurrences of nome, cognome and cf?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct data{
char nome[20];
char cognome[20];
char cf[20];
}data;
void leggi_file(FILE *input, data **array, int *dim);
void aggiorna(data *vect, char *nome, char *cognome, char *cf, int dim);
void scrivi_file(FILE *output, data *vect, int dim);
int main(void){
FILE *input, *output;
input = fopen("prog1file.txt", "r");
output = fopen("prog1file2.txt", "w");
data *vect1;
int dim_vect1 = 0;
leggi_file(input,&vect1,&dim_vect1);
char *nome="Michele", *cognome="Nappi", *cf="nppmhl69p22b119f";
aggiorna(vect1, nome, cognome, cf, dim_vect1);
scrivi_file(output, vect1, dim_vect1);
return 0;
}
void leggi_file(FILE *input, data **array, int *dim){
char nome[20], cognome[20], cf[17];
int i;
while(fscanf(input, "%s %s %s", nome, cognome, cf) == 3)
(*dim)++;
*array = malloc((*dim + 1) * sizeof(data));
rewind(input);
for(i=0; i<(*dim); i++)
fscanf(input, "%s %s %s", array[i]->nome, array[i]->cognome, array[i]->cf);
}
void aggiorna(data *vect, char *nome, char *cognome, char *cf, int dim){
strcpy(vect[dim].nome, nome);
strcpy(vect[dim].cognome, cognome);
strcpy(vect[dim].cf, cf);
}
void scrivi_file(FILE *output, data *vect, int dim){
int i;
for(i=0; i<(dim+1); i++)
fprintf(output, "%s %s %s\n", vect[i].nome, vect[i].cognome, vect[i].cf);
}
It should write the occurrences of nome, cognome and cf that are stored in the first file in the second, and then add another one which was passed from the main. Why does it only write the first and the last?
In the output file I get something like this:
Giuseppe Nappo nppgspmhl65t23c126a
Michele Nappi nppmhl69p22b119f