I want the program to read the details of n employees from the file and store the Salary details of n employees to an array which is dynamically allocated and then print the average salary.
The below program gets the data from the user and stores it in a file.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char name[10];
char nameEr[10];
int salaryannual;
char desig[10];
}Employee;
void main(int argc, char *argv[])
{
int i,n=0,*ptr,sum=0;
n=atoi(argv[1]);
printf("The no.of employee records to be created are: %d\n",n);
FILE *fp;
if((fp=fopen("employee.txt","w"))==NULL)
{
printf("File could not be opened\n");
}
Employee employees[n];
printf("Enter %d Employee Details: \n \n",n);
for(i=0; i<n; i++)
{
printf("Employee %d: \n",i+1);
printf("Employee name: ");
scanf("%s",employees[i].name);
printf("Employer name: ");
scanf("%s",employees[i].nameEr);
printf("Employee designation: ");
scanf("%s",employees[i].desig);
printf("Employee annual salary: ");
scanf("%d",&employees[i].salaryannual);
printf("\n");
}
for(i=0;i<n;i++)
{
fprintf(fp,"%s\n",employees[i].name);
fprintf(fp,"%s\n",employees[i].nameEr);
fprintf(fp,"%s\n",employees[i].desig);
fprintf(fp,"%d\n",employees[i].salaryannual);
}
fclose(fp);
for(i=0;i<n;i++)
{
printf("Employee name \t: ");
printf("%s \n",employees[i].name);
printf("Employer name \t: ");
printf("%s \n",employees[i].nameEr);
printf("Employee designation \t: ");
printf("%s \n",employees[i].desig);
printf("Employee annual salary: ");
printf("%d \n",employees[i].salaryannual);
printf("\n");
}
How do I proceed further from this code. Please help me.
printf("File could not be opened\n");is not enough