Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* E' necessario che venga creato un file di nome input.txt dove ci sono i voti
- intervallati da uno spazio "Quanti ne vuoi รจ del tutto arbitrario".
- E' necessario l'esecuzione avvenga da line di comando come segue:
- "nome-programma.exe", input.txt, output.txt
- E' necessario che il file venga compilato da linea di comando come segue:
- gcc nome-programma.c -o nome-programma.exe
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct studente{
- int matricola;
- char cognome[20];
- char nome[20];
- char luogo[20];
- int anno;
- int *esami;
- }studente;
- /*PROTOTIPI*/
- void controlla_input(int);
- void controlla_file(FILE *,char *);
- void conta_in_file_esami(FILE*,int*);
- void update(char*[],studente *,FILE *);
- void scrivi_file(FILE *,studente,int);
- int main(int argc, char *argv[]){
- char *info[]={"Verde","Luisa","Napoli"};
- studente rec;
- int num_esami = 0;
- FILE *input, *output;
- controlla_input(argc);
- input = fopen(argv[1],"r");
- controlla_file(input,argv[1]);
- output = fopen(argv[2],"w");
- controlla_file(output,argv[2]);
- conta_in_file_esami(input,&num_esami);
- rec.esami = calloc(num_esami,sizeof(int));
- update(info, &rec, input);
- scrivi_file(output,rec,num_esami);
- return 0;
- }
- void controlla_input(int num){
- if(num!=3){
- printf("Likely there is a mismatch in how the program command had to be called\n");
- printf("The right format is : 'Command Name', 'Argument 1', Argument 2");
- exit(EXIT_FAILURE);
- }
- printf("Control input done and is ok.\n");
- }
- void controlla_file(FILE *f,char *file_name){
- if(f==NULL){
- printf("Sorry there are some issue.\n");
- printf("The more likely reasons are :\n");
- printf("Maybe are you typed bad a name? Check the name: %s\n", file_name);
- printf("Maybe haven't you a right permission on file?\n");
- printf("In last, likely be the system downfall:\n");
- }
- printf("Congrats, the file %s is opened successfully\n",file_name);
- }
- void conta_in_file_esami(FILE *f,int *counter){
- int voto;
- while(fscanf(f,"%d",&voto)!=EOF){
- (*counter)++;
- }
- }
- void update(char *dati_parziali[], studente *stud, FILE *f){
- int i=0;
- strcpy(stud->cognome,dati_parziali[0]);
- strcpy(stud->nome,dati_parziali[1]);
- strcpy(stud->luogo,dati_parziali[2]);
- stud->matricola=5128599;
- stud->anno=2017;
- rewind(f);// Serve a portare il puntatore all'inizio file
- // l'ultima scanf lo aveva portato a fine file
- // e sarebbe rimasto li senza il rewinfd perche
- // i file non sono stati ancora chiusi.
- while(fscanf(f,"%d",&stud->esami[i]) != EOF){
- i++;
- }
- }
- void scrivi_file(FILE *f,studente stud,int size){
- int i;
- fprintf(f, "THE STUDENT...\nidentification number: %d\n"
- "Surname: %s\nName. %s\nCity: %s\nYear: %d\n"
- "he has obtained the following grades:\n",
- stud.matricola,
- stud.cognome,
- stud.nome,
- stud.luogo,
- stud.anno);
- for(i=0;i<size;i++){
- fprintf(f,"%d ",stud.esami[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement