Pages

Friday, 9 February 2018

Assume that a Master file contains two fields, Roll no. and name of the student. At the end of the year, a set of students join the class and another set leaves. A Transaction file contains the roll numbers and an appropriate code to add or delete a student. Write a program to create another file that contains the updated list of names and roll numbers. Assume that the Master file and the Transaction file are arranged in ascending order by roll numbers. The updated file should also be in ascending order by roll numbers. Solution: Note:- This program ( all the 3 ) are ideally for single. After you have ran it once, check the results and delete the updated list file before running it again. Assign roll number in ascending orders by roll numbers. And process data according to ascending nature. 1- first program will let you save data in masterfile. 2- second program will let you add or delete data and will generate an updated list in text mode. 3- obtain the updated list and check it ( assuming you have processed data as instructed). 1. program to creat master file.


Assume that a Master file contains two fields, Roll no. and name of the student. At the end of the year, a set of students join the class and another set leaves. A Transaction file contains the roll numbers and an appropriate code to add or delete a student.
Write a program to create another file that contains the updated list of names and roll numbers. Assume that the Master file and the Transaction file are arranged in ascending order by roll numbers. The updated file should also be in ascending order by roll numbers.

Solution:
Note:- This program ( all the 3 ) are ideally for single. After you have ran it once, check the results and delete the updated list file before running it again. Assign roll number in ascending orders by roll numbers. And process data according to ascending nature.

1- first program will let you save data in masterfile.
2- second program will let you add or delete data and will generate an updated list in text mode.
3- obtain the updated list and check it ( assuming you have processed data as instructed).


1. program to creat master file.


#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() {

FILE *fp;
struct student {
  int rl;
  char name[50];
  }s;
char ch,another;
clrscr();

fp=fopen("MASTERFILE.DAT","rb+");

if(fp==NULL) {

fp=fopen("MASTERFILE.DAT","wb+");

if(fp==NULL)
puts("cannot open master file!");
exit();
}


while(1) {

clrscr();

gotoxy(30,2);
printf("Masterfile\n");
gotoxy(30,3);
printf("**********\n\n\n");
gotoxy(20,6);
printf("1: Enter student data: ");
gotoxy(20,8);
printf("2: Read student data: ");
gotoxy(20,10);
printf("0: Exit: ");

gotoxy(2,24);
printf("Your choice: ");
fflush(stdin);

ch=getche();

switch(ch) {

case '1':

clrscr();

fseek(fp,0,SEEK_END);

another='y';

while(another=='y' || another=='Y') {

clrscr();

gotoxy(2,24);
printf("NOTE: assign roll numbers in ascending order");
gotoxy(20,5);
printf("Enter roll number: ");
scanf("%d",&s.rl);

gotoxy(20,7);
printf("Enter name: ");
fflush(stdin);
gets(s.name);

fwrite(&s,sizeof(s),1,fp);



gotoxy(20,10);
printf("Add more data(Y/N): ");
fflush(stdin);

another=getche();

}

break;

case '2':

clrscr();

gotoxy(30,2);
printf("Student data");
gotoxy(30,3);
printf("************\n\n");

rewind(fp);

while(fread(&s,sizeof(s),1,fp)==1) {

printf("\n%d\t%s\n",s.rl,s.name);

}

printf("\n\npress any key to return...");
getch();

break;

case '0':

fclose(fp);
exit();

}
}


}