Project based question (case study)
9.
a. WAP to store teachers’ id, name, address and subject in a data file “teacher.txt”
b. WAP to add/append some more records in same data file “teacher.txt”.
c. WAP to print records stored in data file.
d. WAP to edit particular data stored in data file “teacher.txt”.
e. WAP to delete particular data stored in data file “teacher.txt”.
f. Exit
Convert these individual program into a menu based single program using switch and function.
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
struct Teacher
{
int id;
char name[40];
char address[40];
char subject[40];
}e ;
int menu();
void add(FILE *fp);
void list(FILE *fp);
void update(FILE *fp);
void delRecord(FILE *fp);
void search(FILE *fp);
char another;
int main()
{
FILE *fp;
int f;
// create the file if it does not exist
fp = fopen("teacher.txt", "ab");
if(fp == NULL)
{
puts("Cannot create file");
exit(0) ;
}
fclose(fp);
// now open the file in binary mode for both reading and writing
fp = fopen("teacher.txt", "rb+");
if(fp == NULL)
{
puts("Cannot open file");
exit(0) ;
}
while(1)
{
//clrscr(); // clears the screen and this function work with borland compiler
char choice=menu();
switch(choice)
{
case '1' :
add(fp);
break;
case '2':
list(fp);
break;
case '3' :
update(fp);
break;
case '4':
delRecord(fp);
break;
case '5' :
search(fp);
break;
case '0' :
fclose(fp);
exit(0);
default:
printf("invalid choice!");
}
}
}
int menu()
{
system("cls"); // clears the screen and this function work with gcc compiler
printf("1. Add Records\n");
printf("2. List Records\n");
printf("3. Modify Records\n");
printf("4. Delete Records\n");
printf("5. Search Record\n");
printf("0. Exit\n");
printf("Enter Your Choice : ");
fflush(stdin);
char choice = getche();
return choice;
}
void add(FILE *fp)
{
fseek(fp, 0, SEEK_END) ;
do
{
printf("\n\nEnter Teachers Id: ");
scanf("%d", &e.id);
printf("Enter Teachers Name: ");
scanf("%s", e.name);
printf("Enter Teachers Address: ");
scanf("%s", e.address);
printf("Enter Teachers Subject: ");
scanf("%s", e.subject);
fwrite(&e,sizeof(e), 1, fp);
printf("\nAdd another Record (Y/N) ");
fflush(stdin);
another = getche();
}while(another == 'Y' || another=='y');
}
void list(FILE *fp)
{
rewind(fp);
int f=0;
printf("\n\nID\t\t\tNAME\t\t\tADDRESS\t\t\tSUBJECT\n");
while(fread(&e, sizeof(e), 1, fp) == 1)
{
f=1;
printf("%d\t\t\t%s\t\t\t%s\t\t\t%s\n", e.id, e.name, e.address, e.subject);
}
if(f==0)
{
printf("\nRecord does not exist\n");
}
getch();
}
void update(FILE *fp)
{
char tname[40];
do
{
int f=0;
printf("\n\nEnter name of teacher to modify : ");
scanf("%s", tname);
rewind(fp);
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(strcmpi(e.name, tname) == 0)
{
f=1;
printf("Record found enter new details\n\n");
printf("Enter ID: ");
scanf("%d",&e.id);
printf("Enter Name: ");
scanf("%s", e.name);
printf("Enter Address: ");
scanf("%s", e.address);
printf("Enter Subject: ");
scanf("%s", e.subject);
fseek(fp, -sizeof(e), SEEK_CUR);
fwrite(&e, sizeof(e), 1, fp);
break;
}
}
if(f==0)
{
printf("Record not found\n");
}
printf("\nModify another Record (Y/N) ");
fflush(stdin);
another = getche();
}while(another == 'Y' || another=='y');
}
void delRecord(FILE *fp)
{ char tname[40];
FILE *ft;
do
{
int f=0;
printf("\nEnter name of teacher to delete : ");
scanf("%s", tname);
ft = fopen("temp.txt", "wb");
rewind(fp);
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(strcmpi(e.name, tname) != 0)
{
fwrite(&e, sizeof(e), 1, ft);
}
else
{
f=1;
}
}
if(f==0)
{
printf("Record Not found");
fclose(ft);
remove("temp.txt");
}
else
{
fclose(fp);
fclose(ft);
remove("teacher.txt");
rename("temp.txt", "teacher.txt");
fp = fopen("teacher.txt", "rb+");
printf("Delete another Record (Y/N) ");
}
fflush(stdin);
another = getche();
}while(another == 'Y' || another=='y');
}
void search(FILE *fp)
{
char tname[40];
do
{
int f=0;
printf("\nEnter name of teacher to search ");
scanf("%s", tname);
rewind(fp);
while(fread(&e, sizeof(e), 1, fp) == 1)
{
if(strcmpi(e.name, tname) == 0)
{
printf("%d %s %s %s", e.id, e.name, e.address, e.subject);
f=1;
break;
}
}
if(f==0)
{
printf("Record not found\n");
}
printf("\nSearch another Record (Y/N) ");
fflush(stdin);
another = getche();
}while(another == 'Y' || another=='y');
}
No comments:
Post a Comment