/* ******************************************************* * Author: Nikitas N. Karanikolas, Assistant Professor * * Creation Date: February 21, 2009 * ******************************************************* */ #include #include "types.h" #include "commonf.c" main (int argc, char *argv[]) { personnel_rec e1; char tmp_str[snamesize]; int i, k, howmany; FILE *fp; if (argc<2) { printf("You should provide the name of the file to be created."); exit(1); } fp=fopen(argv[1],"wb"); if (fp==NULL) { printf("Can not open [%s] for writing data.", argv[1]); exit(2); } do { blank(&e1,sizeof(personnel_rec)); printf("\nGive surname: "); gets(e1.sname); printf("[%s]\n",e1.sname); strcpy(tmp_str,e1.sname); upper(tmp_str); if (strcmp(tmp_str,"QUIT")==0) break; printf("\nGive first name: "); gets(e1.fname); printf("[%s]\n",e1.fname); printf("\nGive the speciality: "); gets(e1.speciality); printf("[%s]\n",e1.speciality); printf("\nGive \"year of birth\", \"year of employment\", \"salary\": "); scanf("%d %d %f", &e1.year_of_birth, &e1.year_of_employment, &e1.salary); gets(tmp_str); /* garbage collection */ /* create a unique key for the employee */ sprintf(e1.key, "%c%c%2d%2d", e1.sname[0], e1.fname[0], e1.year_of_birth % 100, e1.year_of_employment %100); if (e1.key[2]==' ') e1.key[2]='0'; /* convert "xy 5 9" to "xy05 9" */ if (e1.key[4]==' ') e1.key[4]='0'; /* convert "xy05 9" to "xy0509" */ upper(e1.key); /* convert "xy0509" to "XY0509" */ printf("the new employee is assigned the key [%s]\n",e1.key); howmany=fwrite(&e1,sizeof(personnel_rec),1,fp); /* printf("\nhowmany=%d\n", howmany); */ } while (1); fclose(fp); }