progC__
23-09-2005, 18:56
...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... :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help:
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... :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: :help: