Sunday, 12 February 2017

Implementation of Sequential File in C++

//Program of sequential file

#include<iostream>
#include<fstream>
using namespace std;
class Myfile
{
    struct student
    {
        int rollno;
        char name[20];
    };
    struct student s;

    public:
    void create(char);
    void display(char);
    void del(char);
    void insert(char);
    void modify(char,int);
};
 void Myfile::create(char fname[20]) //user defined function to create a file and input records in it
{
struct student s;
int i,n;
FILE fp;
fp.fopen(fname,"a+");
cout>>"\nEnter how many no of records you want to input?";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter roll no:";
cin>>rollno;
cout<<"Enter Student Name:"
cin>>s.name;
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
}

 void Myfile::display(char fname[20]) //user defined function to display records
{
struct student s;
FILE fp;
fp=fopen(fname,"r");
if(fp==NULL)
{
cout<<"Error";
}
else
{
while(fread(&s,sizeof(s),1,fp)!=0)
{
cout<<s.rollno;
cout<<s.name<<"\n";
}
}
fclose(fp);
}
void Myfile::create(char fname[20], struct student s) //function to insert a record at end of file
{
ifstream fp;
fp=fopen(fname,"r+");
if(fp==NULL)
{
fp=fopen(fname,"w");
}
else
{
//fseek(fp,0,2);
fwrite(&s,sizeof(s),1,fp);
fclose(fp);
}
}

int Myfile::search(char fname[20],struct student s,int rollno) //function to search a record
{
ifstream *fp;
int count=0;
fp=fopen(fname,"r") ;
while(fread(&s,sizeof(s),1,fp)!=0)
{
if(rollno==s.rollno)
{
return count;
}
else
{
count++;
}
}
return(-1);
fclose(fp);
}

void Myfile::modify(char fname[20],struct student s,int rollno)  //function to modify a particular record
{
int k;
ifstream fp;
fp=fopen(fname,"r+");
while(fread(&s,sizeof(s),1,fp)!=0)
{
if(s.rollno==rollno)
{
fseek(fp,-sizeof(s),1);
cout<<"Enter modified data:\n";
cout<<"Enter roll no:";
cin>>s.rollno;
cout<<"Enter Name:";
cin>>s.name;

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

void Myfile::del(char fname[20],struct student s,int rollno) //function to delete a record
{
int k;
ifstream fp;
ifstream fp1;
fp=fopen(fname,"r");
fp1=fopen("temp","w");
while(fread(&s,sizeof(s),1,fp)!=0)
{
if(rollno!=s.rollno)
{
fwrite(&s,sizeof(s),1,fp1);
}
}
fclose(fp);
fclose(fp1);
remove(fname);
rename("temp",fname);
}



int main()  // call to main function
{
int i,j,k,l; // variable declaration
ifstream fp;
Myfile mf; //declaring a file pointer
int choice;
int rollno;
char fname[20];
struct student s;
cout<<"\nEnter name of file:";

cin>>"%s",fname;
create(fname);

do //use of do while loop to repeat menu
{
cout<<"\n";
cout<<"\n..................MENU.................";
cout<<"\n1.Display\n2.Insert\n3.Search\n4.Modify\n5.Delete\n6.Exit";
cout<<"\n";
cout<<"\nEnter your choice:";
cin>>choice;

switch(choice) //switch case to display menu
{
case 1:
                        cout<<"\nRollno\tName"; //to display record
mf.display(fname);
break;
case 2:
                        cout<<"\nEnter Roll No:"; // to insert a record
cin.rollno;
k=mf.search(fname,s,rollno);
if(k!=-1)    // to avoid duplication
{
cout<<"\nSorry roll no already exist.";
}
else
{
cout<<"\nEnter Name:";
cin>>s.name;
s.rollno=rollno;
mf.inserts()insert(fname,s);
cout<<"Record inserted successfully.";
}
break;
case 3:
                        cout<<"\nEnter the roll no to be searched:"; // to search a record
cin>>rollno;
j=mf.search(fname,s,rollno);
if(j!=-1)
{
cout<<"Record found at position "<<j;
}
else
{
cout<<"Sorry!!!No  such record found.";
}
break;
case 4:
                        cout<<"\nEnter the roll no to be modified:";  //to modify a record
cin>>rollno;
k=mf.search(fname,s,rollno);
if (k!=-1)
{
mf.modify(fname,s,rollno);
}
else
{
cin>>"\nSorry!!!No  such record  found.";
}
break;
case 5:
                        cout<<"\nEnter the roll no to be deleted:"; //to delete a record
cin>>rollno;
l=mf.search(fname,s,rollno);
if (l!=-1)
{
mf.del(fname,s,rollno);
cout<<"Record deleted successfully.";
}
else
{
cout<<"Sorry!!!No  such record found.";
}
break;
case 6:
                        break;
default:
                        cout<<"\nWrong choice.";
break;
} //end of switch case
}while(choice<6); //end of do while loop
cout<<"\n";
    return(0);
} //end of main function


1 comment: