Ken H

Ken H

  • NA
  • 646
  • 361.1k

to write data to a binary file error occurred in c

Jul 9 2013 6:10 AM
hi friend,

    when I input the employee post memory error occurred.
Please help me, thanks.

Code is as follows:

#include<stdio.h>
#include<stdlib.h>
#define EMP_NUM_MAX 100

struct EmployeeInfo  // employee information structure
{
  char name[15];
  int  empID;
  float salary;
  char post[15];
}emp[EMP_NUM_MAX];

// function prototype declaration
void MainMenu(),void WriteData();

// main function
void main(void)
{
   int choice;
   MainMenu();

   printf("\t\tPlease enter your choice:");
   scanf("%d",&choice);

   switch (choice)
   {
       case 1:
    WriteData();
        break;
   }

}

void MainMenu()  // operator menu
{
system("cls"); // clear the screen
printf("\n\t\t **********************************************\n");
printf("\t\t * Employee Salary Management System For V1.0 *\n");
printf("\t\t **********************************************\n\n");
printf("\t\t 1.Add Employee Information\n");
printf("\t\t 2.Display All Employee Information\n");
printf("\t\t 3.Delete Employee Information\n");
printf("\t\t 4.Find Employee Records\n\n");
}

void WriteData()
{
   FILE *fp;
   int emp_num;
   
   printf("\t\t You want to input the number of employees?:");
   scanf("%d",&emp_num);
   for(int i=0;i<emp_num;i++)
   {
      printf("\t\tPlease input employee name:");
      scanf("%s",emp[i].name);
 printf("\t\tPlease input employee ID:");
 scanf("%d",&emp[i].empID);
      printf("\t\tPlease input employee salary:");
 scanf("%f",&emp[i].salary);
 printf("\t\tPlease input employee post:");  // here occurred error
 scanf("%s",emp[i].post);
   }

   if((fp=fopen("EmployeeInfo.dat","ab"))==NULL) // open binary file 
   {
  printf("Can't open file\n");
  exit(1);
   }
   for(int j=0;j<emp_num;j++)
   {   
 if(fwrite(&emp[j],sizeof(struct EmployeeInfo),1,fp)!=1) //writing data to binary file
  printf("Error writing file.\n");
   }
   fclose(fp); // close file point
}

void DeleteEmployeeInfo()
{
int empID;
printf("Please enter the employee's ID to be deleted:");
scanf("%d",&empID);
/*
  here i want to delete the employee ID
*/
}

Answers (7)