Code:
#include <stdio.h>
struct facalty{
int code;
char fac[30];
};
struct student{
int id,tel;
char name[30];
char facl[30];
};
/*prototypes*/
int choose();
void enterStudent( FILE *fptr2,FILE *fptr);
void editStudent( FILE *fptr2);//2
void newFacalty( FILE *fptr);
void listFacalties( FILE *fptr);
void listStudents( FILE *fptr2);//2
int main()
{
FILE *ffac;
FILE *fstud;
int ch;
ffac=fopen("facalty.dat","rb+");//rb+
if(ffac==NULL)
printf("File facalty cannot be created:\n");
fstud=fopen("student.dat","rb+");//rb+
if(fstud==NULL)
printf("File student cannot be created:\n");
while((ch = choose())!=7)
{
switch(ch)
{
case 1:
enterStudent( fstud, ffac);
break;
case 2:
editStudent( fstud );
break;
case 3:
newFacalty( ffac );
break;
case 4:
listFacalties( ffac );
break;
case 5:
listStudents( fstud );
break;
case 6:
//exit(1);
break;
default:
printf("Wrong choice!");
break;
}
}
fclose(ffac);
fclose(fstud);
return 0;
}
int choose()
{
int tmp;
printf("What to do:\n"
"1 - Enter a new record\n"
"2 - Edit a student record\n"
"3 - Enter a new facalty\n"
"4 - List all facalties\n"
"5 - List all students\n"
"6 - exit\n");
scanf("%d",&tmp);
return tmp;
}
void enterStudent( FILE *fptr2,FILE *fptr)
{
int tmprCode;
//char found[30];
struct student st = {0,0,"",""};
struct facalty ft = {0,""};
printf("Enter code for facalty");
scanf("%d",&tmprCode);
if(tmprCode==0)
printf("The code dose not exist (0):");
if (fseek(fptr,(tmprCode-1)*sizeof(struct facalty),SEEK_SET)!=-1){
fread(&ft,sizeof(struct facalty),1,fptr);
//st.facl=ft.fac;
printf("%s for STUDENT\n",st.facl);
printf("%s for FACALTY\n",ft.fac);
printf("Enter ID,telephone,name\n");
scanf("%d %d %s",&st.id,&st.tel,st.name);
fseek(fptr2,(tmprCode-1)*sizeof(struct student),SEEK_SET);//changed 2/student
//fread(&st,sizeof(struct student),1,fptr2);
printf("%d\t%d\t%s\t%s\n",st.id,st.tel,st.name,st.facl);
fwrite(&st,sizeof(struct student),1,fptr2);
printf("Finish writing!");}
else{
printf("The code is not in the list\n\n");
}
}
void editStudent( FILE *fptr2)//2
{
int st_Id;
struct student st_re = {0,0,"",""};
printf("Enter the ID of the student to ubdate the record\n");
scanf("%d",&st_Id);
//rewind(fptr2);
//if(fseek(fptr2,(st_Id-1)*sizeof(struct student),SEEK_SET)==-1)
// printf("The ID is not exists:\n");
//st_re.id=st_Id;
fseek(fptr2,(st_Id-1)*sizeof(struct student),SEEK_SET);
fread(&st_re,sizeof(struct student),1,fptr2);
printf("\nThe record Before SCAN: %d\t%d\t%s\t%s\t\n",st_re.id,st_re.tel,st_re.name,st_re.facl);
if(st_re.id==0)
printf("No information in the record\n");
else
{
printf("Enter the new Telephone, Name, Facalty");
scanf("%d %s %s",&st_re.id, st_re.name, st_re.facl);
//fseek(fptr2,(st_Id-1)*sizeof(struct student),SEEK_SET);
//fread(&st_re,sizeof(struct student),1,fptr2);
fseek(fptr2,(st_Id-1)*sizeof(struct student),SEEK_SET);
printf("\nThe record to write is: %d\t%d\t%s\t%s\t",st_re.id,st_re.tel,st_re.name,st_re.facl);
fwrite(&st_Id,sizeof(struct student),1,fptr2);
printf("\nThe record to write is: %d\t%d\t%s\t%s\t",st_re.id,st_re.tel,st_re.name,st_re.facl);
}
}
void newFacalty( FILE *fptr)
{
struct facalty fac_re = {0," "};
int tmpCode;
printf("Enter code for the new record:\n");
scanf("%d",&tmpCode);
fseek(fptr,(tmpCode-1)*sizeof(struct facalty),SEEK_SET);
fread(&fac_re,sizeof(struct facalty),1,fptr);
if(fac_re.code!=0)
printf("The record contains informatioin\n");
else
{
printf("Enter facalty name\n");
scanf("%s",fac_re.fac);
fac_re.code=tmpCode;
fseek(fptr,(fac_re.code-1)*sizeof(struct facalty),SEEK_SET);
fwrite(&fac_re,sizeof(struct facalty),1,fptr);
}
}
void listFacalties( FILE *fptr)
{
struct facalty fac_re = {0," "};
printf("Code\tFacalty name\n");
rewind(fptr);
while(fread(&fac_re,sizeof(struct facalty),1,fptr))
{
if(fac_re.code!=0)
printf("%d\t%s\n",fac_re.code, fac_re.fac);
}
}
void listStudents( FILE *fptr2)//2
{
struct student st_re = {0,0,"",""};
printf("ID\tTelephone\tName\tFacalty\n");
rewind(fptr2);
while(fread(&st_re,sizeof(struct student),1,fptr2))
{
if(st_re.id!=0)
printf("%d\t%d\t\t%s\t%s\n",st_re.id,st_re.tel,st_re.name,st_re.facl);
}
}