Reputation: 489
This program that I am doing for class to create an EMPLOYEE records file. I created two structs. One called employee and one called date. The EMPLOYEE struct has one char array, one int, 6 float values and DATE(another Struct). The DATE struct has just three int values (month, day, year).
I have created an array of type EMPLOYEE called person[1000].
Here is my code, I keep getting a debug assertion failed error in visual studios pointing to fwrite.c Expression: (Stream !=NULL). I am sure it has to do with my fread or fwite as I have never tried to store structs.
The point of the beginning of this function is to populate the array if the file exists and is not empty, so when the user starts storing data to the array the subscript is updated. I know there are probably a few other issues here but first things first and that would be the reading and writing portion.
Thanks Again,
Mike
void loadPayRoll(EMPLOYEE person[], int *i)
{
char sValidate[5] = "exit";
FILE *f;
int count = 0;
f = fopen("emplyeeRecords.bin", "rb");
if(f){
while(fread(&person[*i], sizeof(person), 1, f) > 0){
(*i)++;
}
fclose(f);
}
else {
while (strcmp( sValidate, person[*i].name)) {
fopen("employeeRecords.bin", "ab+");
printf("Please enter name or type exit to return to main menu: ");
scanf("%s", person[*i].name); //must use the '->' when passing by by refrence, must use '&' sign
flush;
if (!strcmp( sValidate, person[*i].name))
break;
printf("\nPlease enter age of %s: ", person[*i].name);
scanf("%i", &person[*i].age);
flush;
printf("\nPlease enter the hourlyWage for %s: ", person[*i].name);
scanf("%f", &person[*i].hourlyWage);
flush;
printf("\nPlease enter the hours worked for %s: ", person[*i].name);
scanf("%f", &person[*i].hoursWkd);
if (person[*i].hoursWkd > 40) {
person[*i].regPay = person[*i].hoursWkd * 40;
person[*i].otHoursWkd = person[*i].hoursWkd - 40;
person[*i].otPay = person[*i].otHoursWkd * (person[*i].hourlyWage * 1.5);
person[*i].totalPay = person[*i].regPay + person[*i].otPay;
}
else {
person[*i].totalPay = person[*i].hoursWkd * person[*i].hourlyWage;
}
flush;
printf("\nEnter 2 digit month: ");
scanf("%i", &person[*i].payDate.month); //must use the '->' when passing by by refrence, must use '&' sign
flush;
printf("\nEnter 2 digit day: ");
scanf("%i", &person[*i].payDate.day); //must use the '->' when passing by by refrence, must use '&' sign
flush;
printf("\nEnter 4 digit year: ");
scanf("%i", &person[*i].payDate.year); //must use the '->' when passing by by refrence, must use '&' sign
flush;
fwrite(&person[*i], sizeof(person), 1, f);
fclose(f);
(*i)++;
}
}
}//end function loadPayRoll
Upvotes: 1
Views: 1822
Reputation: 66194
I'm pretty sure this:
fopen("employeeRecords.bin", "ab+");
sitting all by its lonesome on a single line without assigning the resulting FILE*
has quite a bit to do with your problem.There are plenty of other issues for example:
flush;
Not really sure what thats all about. Perhaps you meant:
fflush(f);
Assuming f
is ever actually assigned correctly,
And as pointed out in-comment, you should open the file before the loop starts, then write the data as needed, the close it after the loop is finished.
Upvotes: 4