Oct 11, 2011

C Program to do the operations of Sequential file with records.

C Program to do the operations of Sequential file with records. Sequential file, A file which consists of the same record types that is stored on a secondary storage device. The physical sequence of records may be based upon sorting values of one or more data items or fields. Here we are creating, Reading, searching the sequential file, using c file i/o operations. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/

 
#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 (usn,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 USN 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;
}





Read more Similar C Programs
C Basic

C File i/o
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!