|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 39
|
CREAZIONE FILE BINARIO IN C...
...sono di nuovo qui,con un altra inedita difficoltà:
devo scrivere un programma che crea ed aggiorna un file binario "studente" contenete le seguenti informazioni: *cognome e nome (30char) *matricola(ccc/cccc) *numero esami superati (short) *media aritmetica esami *crediti acquisiti il programmino che ho abbozzato io è il seguente: #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ short day; short month; short year; char cognome[15]; char nome [15]; int matricola; short n_esamisup; float media_ar_esami; short cred_acqui; }studente; FILE *fp; /*puntatore a FILE dichiarato come variabile globale*/ studente legg_infor (studente student); void creafile (char nomefile[]); void trasfer_infile (studente student,char nomefile[]); void main () { studente STUDENTE; char name[8]; STUDENTE.cred_acqui = 0; STUDENTE.day = 0; STUDENTE.matricola = 0; STUDENTE.media_ar_esami = 0; STUDENTE.month = 0; STUDENTE.n_esamisup = 0; STUDENTE.year = 0; printf ("inserisci il nome che vuoi attribuire al file (max 8 caratteri)\n"); scanf ("%s",&name); creafile (name); legg_infor (STUDENTE); trasfer_infile (STUDENTE,name); } studente legg_infor (studente student) { printf ("inserisci la data (gg/mm/aaaa)\n"); scanf ("%d%c%d%c%d", &student.day,&student.month,&student.year); printf ("inserisci il cognome dello studente (max 15 caratteri)\n"); scanf ("%s",&student.cognome); printf ("Inserisci il nome dello studente (max 15 caratteri)\n"); scanf ("%s",&student.nome); printf ("inserisci la matricola dello studente\n"); scanf ("%d",&student.matricola); printf ("inserisci gli esami che ha superato\n"); scanf ("%d",&student.n_esamisup); printf ("inserisci la media aritmetica degli esami\n"); scanf ("%f",&student.media_ar_esami); printf ("inserisci i crediti acquisiti\n"); scanf ("%d",&student.cred_acqui); return student; } /*function crea file*/ void creafile (char nomefile[]) { FILE *fp; strcat(nomefile,".txt"); fp = fopen(nomefile,"wb"); if (fp == NULL){ puts ("ERRORE NELLA CREAZIONE DEL FILE"); exit(1); } fclose(fp); } /*function che trasferisce le informazioni immesse nel file creato*/ void trasfer_infile (studente student,char *nomefile) { fp = fopen(nomefile,"rb"); if (fp == NULL){ puts ("ERRORE NEL TRASFERIMENTO DEI DATI NEL FILE: IMPOSSIBILE APRIRE IL FILE!"); exit(1);} fwrite (&student,sizeof (studente),100,fp); fclose (fp); } AIUTATEMI...
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Mar 2002
Città: Empoli (firenze)
Messaggi: 1521
|
ma hai un problema oppure vuoi che ti controlliamo il problema?
__________________
Imac 20" + iphone 4 16gb + MacBook Pro 13" Concluso affari con: bottoni,skino,semmy83,alex_ |
|
|
|
|
|
#3 | |
|
Member
Iscritto dal: Sep 2005
Messaggi: 39
|
Quote:
|
|
|
|
|
|
|
#4 |
|
Moderatore
Iscritto dal: Nov 2003
Messaggi: 16211
|
Ci aiuteresti ad aiutarti se ci dicessi come il programma non funziona.
Ossia: se non stampa il risultato corretto su schermo, se termina improvvisamente, quali messaggi di errore vangono forniti dal compilatore, eccetera.
__________________
Ubuntu è un'antica parola africana che significa "non so configurare Debian" Scienza e tecnica: Matematica - Fisica - Chimica - Informatica - Software scientifico - Consulti medici REGOLAMENTO DarthMaul = Asus FX505 Ryzen 7 3700U 8GB GeForce GTX 1650 Win10 + Ubuntu |
|
|
|
|
|
#5 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 39
|
il file studente sono riuscito a crearlo...adesso ho problemi con la funzione che aggiorna il file... questo algoritmo mi segnala 4 warning e 2 errori che mi stanno facendo impazzire
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ short day; short month; short year; char cognome[15]; char nome [15]; int matricola; // parametri che verranno aggiornati ad ogni data short n_esamisup; // parametro che verrà aggiornato ad ogni data float media_ar_esami; short cred_acqui; }studente; FILE *fp,*fpupdate; //prototipo funzione aggiorna studente studente update (studente STUDENTE); void main () { studente STUDENTE; char nome[10]; //apro il file dati if ((fp = fopen("Michele.dat","rb")) == NULL) { puts ("ERRORE NELL'APERTURA DEL FILE"); exit(1); } //creo il nuovo file aggiornata printf ("Inserisci il nome del nuovo file aggiornato:\n"); fflush(stdin); scanf ("%s",&nome); strcat(nome,".dat"); if ((fpupdate = fopen (nome,"wb")) == NULL) { puts ("ERRORE NELL'APERTURA DEL FILE"); exit(1); } //leggo il primo record creato nel file precedente fread(&STUDENTE,sizeof(studente),1,fp); while (!feof(fp)) /*fino a quando il file "vecchio" non è finito*/ { //richiama la funzione per aggiornare il file STUDENTE = update (STUDENTE); /*scrivo le informazioni aggiornate nel nuovo file*/ fwrite = (STUDENTE,sizeof(studente),1,fpupdate); //legge record successivo nel file dati precedente fread = (STUDENTE,sizeof(studente),1,fp); } fclose(fp); fclose(fpupdate); } studente update (studente STUDENTE) { printf ("Inserisci la data (gg/mm/aaaa):\n"); scanf ("%d%c%d%c%d", &STUDENTE.day,&STUDENTE.month,&STUDENTE.year); printf ("Inserisci il cognome dello studente (max 15 caratteri)\n"); scanf ("%s",&STUDENTE.cognome); printf ("Inserisci il nome dello studente (max 15 caratteri)\n"); scanf ("%s",&STUDENTE.nome); printf ("Inserisci la matricola dello studente\n"); scanf ("%d",&STUDENTE.matricola); printf ("Inserisci gli esami che ha superato\n"); scanf ("%d",&STUDENTE.n_esamisup); printf ("Inserisci la media aritmetica degli esami\n"); scanf ("%f",&STUDENTE.media_ar_esami); printf ("Inserisci i crediti acquisiti\n"); scanf ("%d",&STUDENTE.cred_acqui); return (STUDENTE); } Gli errori e warning stanno tutti dove il carattere è rosso... |
|
|
|
|
|
#6 |
|
Member
Iscritto dal: Sep 2005
Messaggi: 39
|
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:47.



















