#include <stdio.h>
#include <stdlib.h>
#define LUNGNOM 30
typedef struct dipendenti {
char NomeCognome[LUNGNOM];
char Codice[9];
short ore;
float media;
} s_di;
void main()
{
int i;
short sup;
s_di impiegati;
FILE *fp;
char NomCog[LUNGNOM], Cod[9];
//Apertura del file con controllo
if(( fp=fopen("impiegati.dat", "wb"))==NULL)
{
puts("Errore nell'apertura del file");
exit(1);
}
//In caso di esito positivo
printf("Inserisci il Nome e Cognome dell'impiegato: ");
gets(impiegati.NomeCognome);
fseek(fp,0,SEEK_SET);
fwrite(impiegati.NomeCognome, sizeof(char), LUNGNOM, fp);
printf("\nInserisci l'ID dell'impiegato (ccc/cccc): ");
gets(impiegati.Codice);
fseek(fp,LUNGNOM,SEEK_SET);
fwrite(impiegati.Codice, sizeof(char), 9, fp);
printf("\nInserisci le ore di lavoro: ");
scanf("%hd",impiegati.ore);
fseek(fp,LUNGNOM+9,SEEK_SET);
fwrite(impiegati.ore, sizeof(short),1,fp);
fclose(fp);
//Apertura del file con controllo
if(( fp=fopen("impiegati.dat", "rb"))==NULL)
{
puts("Errore nell'apertura del file");
exit(1);
}
fseek(fp,0,SEEK_SET);
fread(NomCog, sizeof(char), LUNGNOM, fp);
puts(NomCog);
fseek(fp, 30, SEEK_SET);
if(fread(Cod, sizeof (char), 8, fp)!=8)
puts("ERRORE!!");
puts(Cod);
fseek(fp,LUNGNOM+9,SEEK_SET);
fread(&sup,sizeof(short),1,fp);
printf("%hd\n", sup);
fclose(fp);
system("PAUSE");
}