Friday, February 6, 2015

IMPLEMENTATION OF SEQUENTIAL ACCESS FILE

AIM

            To write a C program to implement sequential access of file

ALGORITHM

Step-1: Start the program
Step-2: Open the student file in writing mode
Step-3: Get the user id, name and marks of the students
Step-4: Close the file and open the file in read mode
Step-5: For the search option, user id is given. If the record is found the details of the  
            Student of the student is displayed else failure will be displayed.
Step-6: The display option gives the entire details of the student
Step-7: End the program in the main function

PROGRAM
#include <stdio.h>
typedef struct
{
 int usn;
 char name[25];
 int m1,m2,m3;
}STD;
STD s;
void display(FILE *);
int search(FILE *,int);
void main()
{
 int i,n,usn_key,opn;
 FILE *fp;
 printf(" How many Records ? ");
 scanf("%d",&n);
 fp=fopen("stud.dat","w");
 for(i=0;i<n;i++)
 {
  printf("Read the Info for Student: %d (uid,name,m1,m2,m3) \n",i+1);
  scanf("%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
  fwrite(&s,sizeof(s),1,fp);
 }
 fclose(fp);
 fp=fopen("stud.dat","r");
 do
 {
  printf("Press 1- Display\t 2- Search\t 3- Exit\t Your Option?");
  scanf("%d",&opn);
  switch(opn)
  {
  case 1: printf("\n Student Records in the File \n");
  display(fp);
  break;
  case 2: printf(" Read the uid of the student to be searched ?");
  scanf("%d",&usn_key);
  if(search(fp,usn_key))
  {
   printf("Success ! Record found in the file\n");
   printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
  }
  else
   printf(" Failure!! Record with USN %d not found\n",usn_key);
  break;
  case 3:  printf(" Exit!! Press a key . . .");
  getch();
  break;
  default:  printf(" Invalid Option!!! Try again !!!\n");
  break;
  }
 }while(opn != 3);
 fclose(fp);
}   /* End of main() */
void display(FILE *fp)
{
 rewind(fp);
 while(fread(&s,sizeof(s),1,fp))
  printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp, int usn_key)
{
 rewind(fp);
 while(fread(&s,sizeof(s),1,fp))
  if( s.usn == usn_key) return 1;
 return 0;
}







OUTPUT
How many Records ?4
Read the Info for Student: 1 (uid,name,m1,m2,m3)
5
allen
68
59
40
Read the Info for Student: 2 (uid,name,m1,m2,m3)
2
briane
89
58
49
Read the Info for Student: 3 (uid,name,m1,m2,m3)
44
kernighan
44
69
58
Read the Info for Student: 4 (uid,name,m1,m2,m3)
8
weiss
95
48
69
Press 1- Display         2- Search       3- Exit         Your Option?1

 Student Records in the File
5       allen   68      59      40
2       briane  89      58      49
44      kernighan       44      69      58
8       weiss   95      48      69
Press 1- Display         2- Search       3- Exit         Your Option?2
 Read the uid of the student to be searched ?44
Success ! Record found in the file
44      kernighan       44      69      58
Press 1- Display         2- Search       3- Exit         Your Option?3
 Exit!! Press a key . . .




RESULT: Thus a c program for sequential access file is implemented successfully.


No comments:

Post a Comment