Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- typedef struct data{
- char nome[20];
- char cognome[20];
- char cf[20];
- }data;
- void controllo_input(int);
- void controllo_file(FILE*);
- void leggi_file(FILE*, data**, int*);
- void aggiorna(data*, char*, char*, char*, int);
- void scrivi_file(FILE*, data*, int);
- int main(int argc,char *argv[]){
- FILE *one, *two;
- data *array;
- int dim;
- char nome[20], cognome[20], cf[20];
- controllo_input(argc);
- one=fopen(argv[1],"r");
- controllo_file(one);
- two=fopen(argv[2],"w");
- controllo_file(two);
- leggi_file(one, &array, &dim);
- printf("\n Inserire nome - cognome - cf: ");
- scanf("%s%s%s", nome, cognome, cf);
- aggiorna(array, nome, cognome, cf, dim);
- scrivi_file(two, array, dim);
- printf("\n");
- fclose(one);
- fclose(two);
- return 0;
- }
- void controllo_input(int argc){
- if(argc!=3){
- fprintf(stdout, "\n errore, mancanza di file. \n");
- exit(EXIT_FAILURE);
- } }
- void controllo_file(FILE *file){
- if(file==NULL){
- fprintf(stdout, "\n errore apertura file. \n");
- exit(EXIT_FAILURE);
- } }
- void leggi_file(FILE *input, data **array, int *dim){
- data in;
- while(fscanf(input, "%s%s%s", in.nome, in.cognome, in.cf)!=EOF){
- (*dim)++;
- }
- *array=calloc(*(dim+1), sizeof(data));
- for(int i=o; i<*(dim+1); i++){
- fscanf(input, "%s%s%s", array[0][i].nome, array[0][i].cognome, array[0][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){
- fprintf(output, "%s %s %s", vect[dim].nome, vect[dim].cognome, vect[dim].cf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement