thanks for the useful links. So the result that i got is this
Code:
#include<stdio.h>
#include<string.h>
#define size 5
typedef struct {
char last[10]; /* Structure of type student*/
char first[10];
double gpa;
} stud;
typedef struct {
char last[10];
char first[10]; /* structure of type employee*/
double salary;
} emp;
void check_add_salary (emp *e, stud *s);
main(){
FILE *stud_file=NULL;
FILE *employ_file=NULL;
FILE *employ_file2=NULL;
int i=0 , j=0;
stud arr_stud[size]; /* An array of structures (students)*/
emp arr_emp[size]; /*An array of structures (employees)*/
if((stud_file=fopen("student.txt","r"))==NULL)
printf("\nThe file could not be opened\n\n");
else
{ fseek(stud_file,50,SEEK_SET);
while(!feof(stud_file))
{fscanf(stud_file,"%s%s%lf\n", arr_stud[i].last,arr_stud[i].first,&arr_stud[i].gpa);
i++;
}
fclose(stud_file);
}
/*end of else*/
i=0;
if((employ_file=fopen("employees.txt","r"))==NULL)
{
printf("\nThe file could not be opened\n\n");
}
else
{
fseek(employ_file,51,SEEK_SET);
while(!feof(stud_file))
{
fscanf(employ_file,"%s%s%lf\n", arr_emp[i].last,arr_emp[i].first,&arr_emp[i].salary);
i++;
}
fclose(employ_file);
}
/***end of reading from file employess*/
check_salary(arr_emp,arr_stud);
if((employ_file2=fopen("update.txt","w"))==NULL)
{
printf("\nThe file could not be opened\n\n");
}
else
{ fputs("\t\tThe new list of employees(after salary change)\n\n",employ_file2);
fputs("Last name\tFirst name\t\tGPA\n",employ_file2);
fputs("************************************************\n",employ_file2);
for(i=0;i<size;i++)
fprintf(employ_file2,"%-10s %-10s %-10.2f\n", arr_emp[i].last,arr_emp[i].first,arr_emp[i].salary); /* Display the list of employees sorted by alphabetical order*/
fputs("\n************************************************\n\n\n\n",employ_file2);
fclose(employ_file2);
printf("\n The file of employees with the new salary has been created n\" );
}
void check_salary (emp *r, stud *s)
{
int i , j;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
if( strcmp (e[i].last,s[j].last)==0 && strcmp(e[i].first,s[j].first)==0 )
if(s[i].gpa>3.0) /* The if statement is to add 10% to the salary*/
{ /*of employees whose students have GPA>3*/
r[i].salary = r[i].salary + ((r[i].salary)*0.1); j=size-1;
}
}
but by using fseek its no more sequential . Anyone can tell how to make sequential?
Thanks