Jan

23

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

struct emp_record{

int eno;

char ename[20];

float bpay;

}erec;

FILE *efile;

//———————————————-

void main(){

void createfile(void);

void readfile(void);

void appendrec(void);

void modirec(void);

void deleterec(void);

int n;

do

{

printf(“\n\t Main Menu”);

printf(“\n\t Press 1 – file creation”);

printf(“\n\t Press 2 – reading”);

printf(“\n\t Press 3 – appending”);

printf(“\n\t Press 4 – modify a record”);

printf(“\n\t Press 5 – delete a record”);

printf(“\n\t Press 6 – quit”);

printf(“\n\n Enter your choice: “);

scanf(“%d”,&n);

switch(n){

case 1: createfile();

break;

case 2: readfile();

break;

case 3: appendrec();

break;

case 4: modirec();

break;

case 5: deleterec();

break;

case 6: exit(0);

break;

}

}while(n != 6);

}

//———END MAIN————————-

//—————- file creation ————–

void createfile(void){

char choice=’y’;

efile=fopen(“employ.dat”,”w”);

fflush(stdin);

while(toupper(choice)==’Y’){

printf(“\n Enter the employee No: “);

scanf(“%d”,&erec.eno);

printf(“\n Enter the employee Name: “);

scanf(“%s”,erec.ename);

printf(“\n Enter the employee Basic Pay: “);

scanf(“%f”,&erec.bpay);

fwrite(&erec,sizeof(erec),1,efile);

printf(“\n Press y – to continue “);

printf(“\n Any other key to stop. “);

choice=getche();

}

fclose(efile);

}

//————————————————–

//—————- Read file ———————–

void readfile(void){

efile=fopen(“employ.dat”,”r”);

printf(“\n—————————————–“);

printf(“\nEmp No. Employee Name Basic Pay”);

printf(“\n—————————————–“);

fread(&erec,sizeof(erec),1,efile);

while(!feof(efile))

{

printf(“\n %d \t %-20s %-0.2f”,erec.eno,erec.ename,erec.bpay);

fread(&erec,sizeof(erec),1,efile);

}

printf(“\n—————————————–\n”);

fclose(efile);

printf(“\n Press ant key to continue …”);

getch();

}

//——————————————————

//————— Append records ———————–

void appendrec(void){

char choice=’y’;

efile=fopen(“employ.dat”,”a”);

fflush(stdin);

while(toupper(choice)==’Y’){

printf(“\n Enter the employee No: “);

scanf(“%d”,&erec.eno);

printf(“\n Enter the employee Name: “);

scanf(“%s”,erec.ename);

printf(“\n Enter the employee Basic Pay: “);

scanf(“%f”,&erec.bpay);

fwrite(&erec,sizeof(erec),1,efile);

printf(“\n Press y – to continue “);

printf(“\n Any other key to stop. “);

choice=getche();

}

fclose(efile);

}

//——————————————————

//—————- Modify Record ———————–

void modirec(void){

int temp,flag=0,n;

efile=fopen(“employ.dat”,”r+”);

fflush(stdin);

printf(“\n To modify a record”);

printf(“————————“);

printf(“\n Enter the Roll No.”);

scanf(“%d”,&temp);

rewind(efile);

fread(&erec,sizeof(erec),1,efile);

while(!feof(efile) && flag==0){

if(erec.eno==temp)

{

printf(“\n Existing record details “);

printf(“\n %d \t %-20s %-0.2f”,erec.eno,erec.ename,erec.bpay);

fflush(stdin);

printf(“\n\n Enter the modified details “);

printf(“\n——————————–“);

printf(“\n Enter the employee No: “);

scanf(“%d”,&erec.eno);

printf(“\n Enter the employee Name: “);

scanf(“%s”,erec.ename);

printf(“\n Enter the employee Basic Pay: “);

scanf(“%f”,&erec.bpay);

n=sizeof(erec);

fseek(efile,-n,SEEK_CUR);

fwrite(&erec,n,1,efile);

flag=1;

printf(“\n The record has been modified!”);

}

fread(&erec,sizeof(erec),1,efile);

}

fclose(efile);

if(flag==0)

printf(“\n The given record is not present in the file!!!”);

printf(“\n\t press any key to continue … “);

getch();

}

//————————————————

//————– Delete Record ——————-

void deleterec(void){

int temp,flag=0;

FILE *tfile;

efile=fopen(“employ.dat”,”r”);

tfile=fopen(“temp.dat”,”w”);

printf(“\n To delete a record “);

printf(“\n———————“);

printf(“\n Enter the Roll No. “);

fflush(stdin);

scanf(“%d”,&temp);

fread(&erec,sizeof(erec),1,efile);

while(!feof(efile))

{

if(erec.eno == temp)

{

printf(“\n The given record “);

printf(“\n %d \t %-20s %-0.2f”,erec.eno,erec.ename,erec.bpay);

printf(“\n is deleted !!!”);

flag = 1;

}

else{

fwrite(&erec,sizeof(erec),1,tfile);

}

fread(&erec,sizeof(erec),1,efile);

}

fclose(tfile);

fclose(efile);

remove(“employ.dat”);

rename(“temp.dat”,”employ.dat”);

if(flag == 0)

printf(“\n The given record is not present in thr file!!!”);

printf(“\n\t press any key to continue”);

getch();

}

//————————————————


Leave a Reply