C - Program to Implement Sequential File (File Handling)
#include <stdio.h>
#include <conio.h>
typedef struct student
{
int rollno;
char name[20];
float marks;
int status;
}student;
void create();
void read();
void insert(student rec1);
int Delete(int rollno);
int search(int rollno);
void pack();
void update();
void main()
{
FILE *master;
int rollno,op,recno;
student rec1;
create();
do
{
printf("\n\n1)Read(Display)\n2)Insert\n3)Delete\n4)Update");
printf("\n5)Search\n6)Pack\n7)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: read();break;
case 2: printf("\nEnter a record to be inserted(roll no,name,marks) : ");
scanf("%d%s%f",&rec1.rollno,rec1.name,&rec1.marks);
insert(rec1);
break;
case 3: printf("\nEnter the roll no.:");
scanf("%d",&rollno);
Delete(rollno);
break;
case 4: update(); break;
case 5: printf("\nEnter a roll no. : ");
scanf("%d",&rollno);
recno=search(rollno);
if(recno>=0)
{
printf("\n Record No.: %d",recno);
master=fopen("master.txt","rb");
fseek(master,recno*sizeof(student),0);
fread(&rec1,sizeof(student),1,master);
printf("\n%d %s %5.2f",rec1.rollno,rec1.name,rec1.marks);
}
else
printf("\nRecord Not Found ");
break;
case 6: pack();break;
}
}while(op!=7);
}
void create()
{
FILE *master;
if(!(master=fopen("master.txt","rb")))
master=fopen("master.txt","wb");
close(master);
}
void read()
{
FILE *master;
student crec;
int i=1,n;
master=fopen("master.txt","r+b");
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);//No. of records
rewind(master);
for(i=1;i<=n;i++)
{
fread(&crec,sizeof(student),1,master);
if(crec.status==0)//if the record is not logically deleted
printf("\n%d) %d\t%s\t%7.2f",i,crec.rollno,crec.name,crec.marks);
else
printf("\n%d) ****** deleted *********",i);
}
}
void insert(student rec1)
{
FILE *master;
student crec;
int n,i,k;
master=fopen("master.txt","r+b");
rec1.status=0;
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);//No. of records
if(n==0)//empty file
{
fwrite(&rec1,sizeof(student),1,master);
fclose(master);
return;
}
/* Shift records until the point of insertion */
i=n-1;
while(i>=0)
{
fseek(master,i*sizeof(student),0);
flushall();
fread(&crec,sizeof(student),1,master);
if(crec.rollno>rec1.rollno)
{
fseek(master,(i+1)*sizeof(student),0);
fwrite(&crec,sizeof(student),1,master);
}
else
break;
i--;
}
flushall();
/*insert the record at (i+1)th position */
i++;
printf("\ni=%d",i);
fseek(master,i*sizeof(student),0);
fwrite(&rec1,sizeof(student),1,master);
fclose(master);
}
int Delete(int rollno)
{
FILE *master;
student crec;
int i,n;
master=fopen("master.txt","r+b");
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);
rewind(master);
for(i=0;i<n;i++)
{
fread(&crec,sizeof(student),1,master);
if(crec.status==0)
{
if(crec.rollno>rollno)
{
printf("\nRecord does not exist ...");
close(master);
return(0);
}
if(crec.rollno==rollno)
{
crec.status=1;
fseek(master,i*sizeof(student),0);
fwrite(&crec,sizeof(student),1,master);
fclose(master);
return(1);
}
}
}
return(0);
}
int search(int rollno)
{
FILE *master;
student crec;
int i,n;
master=fopen("master.txt","r+b");
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);
rewind(master);
for(i=0;i<n;i++)
{
fread(&crec,sizeof(student),1,master);
if(crec.status==0)
{
if(crec.rollno>rollno)
{
fclose(master);
return(-1);
}
if(crec.rollno==rollno)
{
fclose(master);
return(i);
}
}
}
return(-1);
}
void pack()
{
FILE *master,*temp;
student crec;
int i,n;
master=fopen("master.txt","rb");
temp=fopen("temp.txt","wb");//temporary file to copy the remaining records
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);
rewind(master);
for(i=0;i<n;i++)
{
fread(&crec,sizeof(student),1,master);
if(crec.status==0)
fwrite(&crec,sizeof(student),1,temp);
}
fclose(master);
fclose(temp);
//copy records back from temp file to master
temp=fopen("temp.txt","rb");
master=fopen("master.txt","wb");
fseek(temp,0,2);/*go to the end of file */
n=ftell(temp)/sizeof(student);
rewind(temp);
for(i=0;i<n;i++)
{
fread(&crec,sizeof(student),1,temp);
fwrite(&crec,sizeof(student),1,master);
}
fclose(master);
fclose(temp);
}
void update()
{
int rollno;
student rec1;
printf("\n Enter the rollno of the record to be updated : ");
scanf("%d",&rollno);
printf("\nEnter a new record(roll no. name marks) : ");
scanf("%d%s%f",&rec1.rollno,rec1.name,&rec1.marks);
if(Delete(rollno))
insert(rec1);
else
printf("\n Record not found :");
}
#include <conio.h>
typedef struct student
{
int rollno;
char name[20];
float marks;
int status;
}student;
void create();
void read();
void insert(student rec1);
int Delete(int rollno);
int search(int rollno);
void pack();
void update();
void main()
{
FILE *master;
int rollno,op,recno;
student rec1;
create();
do
{
printf("\n\n1)Read(Display)\n2)Insert\n3)Delete\n4)Update");
printf("\n5)Search\n6)Pack\n7)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: read();break;
case 2: printf("\nEnter a record to be inserted(roll no,name,marks) : ");
scanf("%d%s%f",&rec1.rollno,rec1.name,&rec1.marks);
insert(rec1);
break;
case 3: printf("\nEnter the roll no.:");
scanf("%d",&rollno);
Delete(rollno);
break;
case 4: update(); break;
case 5: printf("\nEnter a roll no. : ");
scanf("%d",&rollno);
recno=search(rollno);
if(recno>=0)
{
printf("\n Record No.: %d",recno);
master=fopen("master.txt","rb");
fseek(master,recno*sizeof(student),0);
fread(&rec1,sizeof(student),1,master);
printf("\n%d %s %5.2f",rec1.rollno,rec1.name,rec1.marks);
}
else
printf("\nRecord Not Found ");
break;
case 6: pack();break;
}
}while(op!=7);
}
void create()
{
FILE *master;
if(!(master=fopen("master.txt","rb")))
master=fopen("master.txt","wb");
close(master);
}
void read()
{
FILE *master;
student crec;
int i=1,n;
master=fopen("master.txt","r+b");
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);//No. of records
rewind(master);
for(i=1;i<=n;i++)
{
fread(&crec,sizeof(student),1,master);
if(crec.status==0)//if the record is not logically deleted
printf("\n%d) %d\t%s\t%7.2f",i,crec.rollno,crec.name,crec.marks);
else
printf("\n%d) ****** deleted *********",i);
}
}
void insert(student rec1)
{
FILE *master;
student crec;
int n,i,k;
master=fopen("master.txt","r+b");
rec1.status=0;
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);//No. of records
if(n==0)//empty file
{
fwrite(&rec1,sizeof(student),1,master);
fclose(master);
return;
}
/* Shift records until the point of insertion */
i=n-1;
while(i>=0)
{
fseek(master,i*sizeof(student),0);
flushall();
fread(&crec,sizeof(student),1,master);
if(crec.rollno>rec1.rollno)
{
fseek(master,(i+1)*sizeof(student),0);
fwrite(&crec,sizeof(student),1,master);
}
else
break;
i--;
}
flushall();
/*insert the record at (i+1)th position */
i++;
printf("\ni=%d",i);
fseek(master,i*sizeof(student),0);
fwrite(&rec1,sizeof(student),1,master);
fclose(master);
}
int Delete(int rollno)
{
FILE *master;
student crec;
int i,n;
master=fopen("master.txt","r+b");
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);
rewind(master);
for(i=0;i<n;i++)
{
fread(&crec,sizeof(student),1,master);
if(crec.status==0)
{
if(crec.rollno>rollno)
{
printf("\nRecord does not exist ...");
close(master);
return(0);
}
if(crec.rollno==rollno)
{
crec.status=1;
fseek(master,i*sizeof(student),0);
fwrite(&crec,sizeof(student),1,master);
fclose(master);
return(1);
}
}
}
return(0);
}
int search(int rollno)
{
FILE *master;
student crec;
int i,n;
master=fopen("master.txt","r+b");
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);
rewind(master);
for(i=0;i<n;i++)
{
fread(&crec,sizeof(student),1,master);
if(crec.status==0)
{
if(crec.rollno>rollno)
{
fclose(master);
return(-1);
}
if(crec.rollno==rollno)
{
fclose(master);
return(i);
}
}
}
return(-1);
}
void pack()
{
FILE *master,*temp;
student crec;
int i,n;
master=fopen("master.txt","rb");
temp=fopen("temp.txt","wb");//temporary file to copy the remaining records
fseek(master,0,2);/*go to the end of file */
n=ftell(master)/sizeof(student);
rewind(master);
for(i=0;i<n;i++)
{
fread(&crec,sizeof(student),1,master);
if(crec.status==0)
fwrite(&crec,sizeof(student),1,temp);
}
fclose(master);
fclose(temp);
//copy records back from temp file to master
temp=fopen("temp.txt","rb");
master=fopen("master.txt","wb");
fseek(temp,0,2);/*go to the end of file */
n=ftell(temp)/sizeof(student);
rewind(temp);
for(i=0;i<n;i++)
{
fread(&crec,sizeof(student),1,temp);
fwrite(&crec,sizeof(student),1,master);
}
fclose(master);
fclose(temp);
}
void update()
{
int rollno;
student rec1;
printf("\n Enter the rollno of the record to be updated : ");
scanf("%d",&rollno);
printf("\nEnter a new record(roll no. name marks) : ");
scanf("%d%s%f",&rec1.rollno,rec1.name,&rec1.marks);
if(Delete(rollno))
insert(rec1);
else
printf("\n Record not found :");
}