Structure
Code:
struct EmployeeData
{
int code; /* Employee Code */
char * name; /* Employee Name */
int age; /* Employee Age */
double basicPay; /* Basic pay of Employee */
char jobCat; /* Job Category in char of Employee */
int hours; /* Hour works by Employee */
struct Date * dob; /* Date of birth data element */
struct Pay * dataElement; /* Employee Pay data element */
struct EmployeeData * nextElement; /* Linked List, next element */
};
/*
Structure for Pay data
*/
struct Pay{
int otHours; /* Overtime hours */
double weeklyPay; /* Weekly pay */
double otPay; /* Overtime pay */
};
/*
Structure for Date
*/
struct Date{
int month; /* Month of year */
int day; /* Day of month */
int year; /* year */
};
Full Method body
Code:
void saveEmployeeDataByBinary(struct EmployeeData *emp)
{
FILE *file;
char choice;
char fileName[100];
struct EmployeeData *head = emp;
int i;
if(emp == NULL)//If list is empty
{
printf("\n\tNo Employee Have been added!\n\n\t");
return;
}
while(emp != NULL){
//Check if all data are computed
if (emp->dataElement->weeklyPay == 0){
printf("\n\tOne or more of the employee pay have not been computed, \n\tplease compute before saving.\n\t");
return;
}
else
emp = emp->nextElement;
}
do{
printf("\t__________________________________________________________________\n");
printf("\n\tEnter the full file path that you would like to save the file to \n");
printf("\n\te.g. C:\\Temp\\Employee.BIN\n");
printf("\n\tNOTE THE .BIN EXTENSION\n\n");
printf("\t__________________________________________________________________\n");
printf("\tFile path: ");
gets(fileName);
fflush(stdin);
for(i = 0; fileName[i]; i++){
fileName[i] = tolower(fileName[i]);
}
if (strstr(fileName, ":\\") == NULL){
cls();
printf("\n\tNo Drive Was Being Specified\n\n");
}
else if(strstr(fileName, ".bin" )== NULL){
cls();
printf("\n\tInvalid file extension\n\n");
}
}while((strstr(fileName, ".bin" )== NULL) || (strstr(fileName, ":\\") == NULL));
if(fopen(fileName, "r") != NULL)// check if specified file exists
{
do{
printf("\n\tFile already exists!");
printf("\n\n\tA. Overwrite\n\n\tB. Append \n\n\tC. Close? \n");
printf("\n\tEnter choice: ");
scanf("%c",&choice);
fflush(stdin);
//ask user if he would like to append, overwrite or close and end the operation
switch(choice){
case 'a': case 'A':
file = fopen(fileName, "wb"); //overwrite
break;
case 'B': case 'b':
file = fopen(fileName, "ab"); //Append
break;
case 'c': case 'C':
printf("\t");
return; //End operation
break;
default:
printf("\n\tWrong choice submitted, please try again.\n");
break;
}
}while ((choice!='a') && (choice!= 'b') && (choice!='c') && (choice!= 'A') && (choice!='B') && (choice!= 'C'));
}
else
{
file = fopen(fileName, "wb"); //open for write
}
if(file != NULL){
while(head != NULL){
fwrite(head->code,sizeof(head->code),1,file);
fwrite(head->name, sizeof(head->name),1,file);
fwrite(head->dob->day, sizeof(head->dob->day),1,file);
fwrite(head->dob->month, sizeof(head->dob->month),1,file);
fwrite(head->dob->year, sizeof(head->dob->year),1,file);
fwrite(head->jobCat, sizeof(head->jobCat),1,file);
fwrite(head->basicPay,sizeof(head->basicPay),1,file);
fwrite(head->hours, sizeof(head->hours),1,file);
fwrite(head->dataElement->otHours, sizeof(head->dataElement->otHours),1,file);
fwrite(head->dataElement->otHours, sizeof(head->dataElement->otPay),1,file);
fwrite(head->dataElement->otHours, sizeof(head->dataElement->weeklyPay),1,file);
head = head->nextElement;
}
if(fclose(file)){ //if file fails to close
printf("\n\tError during write operation!\n\n\t");
}
else{
printf("\n\tFile Saved Successfully!\n\n\t");
}
}
else{
printf("\n\tError during write operation!\n\n\t");
}
}