Вы находитесь на странице: 1из 12

#include<fstream.

h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
fstream f1,f2;
int ch,n,i;
char ans;
class book
{
int bookid,noofpages;
char name[20];
public:
void getd()
{
cout<<"\nEnter book id:";
cin>>bookid;
cout<<"Enter name of book:";
gets(name);
cout<<"Enter number of pages:";
cin>>noofpages;
}
void display()
{
cout<<"\nBook ID:"<<bookid<<"\nName:"<<name
<<"\nPages="<<noofpages;
}
int retid()
{
return bookid;
}
char* retname()
{
return name;
}
}obj;
void create()
{
f1.open("Data.dat",ios::out|ios::binary);
do
{
obj.getd();
f1.write((char*)&obj,sizeof(book));
cout<<"Do you wish to enter more?(y/n):";
cin>>ans;
}while(ans=='y'||ans=='Y');
f1.close();
cout<<"\nFile Successfully Created";
}
void append()
{
f1.open("Data.dat",ios::app|ios::binary);
do
{
obj.getd();
f1.write((char*)&obj,sizeof(book));
cout<<"Do you wish to enter more?(y/n):";

cin>>ans;
}while(ans=='y'||ans=='Y');
cout<<"File successfully Appended";
f1.close();
}
void read()
{
f1.open("Data.dat",ios::in|ios::binary);
cout<<"\nContents of the file:";
while(f1.read((char*)&obj,sizeof(book)))
{
cout<<endl;
obj.display();
getch();
}
f1.close();
}
void rewrite()
{
f1.open("Data.dat",ios::out|ios::binary);
f2.open("temp.dat",ios::in|ios::binary);
while(f2.read((char*)&obj,sizeof(book)))
{
f1.write((char*)&obj,sizeof(book));
}
f1.close();
f2.close();
remove("temp.dat");
}
void edit()
{cout<<"\nEnter the record number to be edited:";
cin>>n;
i=1;
f1.open("Data.dat",ios::in|ios::binary);
f2.open("temp.dat",ios::out|ios::binary);
while(f1.read((char*)&obj,sizeof(book)))
{
if(i!=n)
{
f2.write((char*)&obj,sizeof(book));
i++;
}
else
{
cout<<"\nEnter the Details";
obj.getd();
f2.write((char*)&obj,sizeof(book));
i++;
}
}
f1.close();
f2.close();
rewrite();
cout<<"\nRecord number "<<n<<" edited successfully";
}
void delrec()

{
cout<<"\nEnter the record number to be deleted:";
cin>>n;
i=1;
f2.open("temp.dat",ios::out|ios::binary);
f1.open("Data.dat",ios::in|ios::binary);
while(f1.read((char*)&obj,sizeof(book)))
{
if(i!=n)
{
f2.write((char*)&obj,sizeof(book));
i++;
}
else
i++;
}
f1.close();
f2.close();
rewrite();
cout<<"\nRecord number "<<n<<" deleted from file";
}
void insrec()
{
book b1;
cout<<"Enter the position to enter record:";
cin>>n;
i=1;
f1.open("Data.dat",ios::in|ios::binary);
f2.open("temp.dat",ios::out|ios::binary);
while(f1.read((char*)&obj,sizeof(book)))
{
if(n!=i)
{
f2.write((char*)&obj,sizeof(book));
i++;
}
else
{
cout<<"\nEnter the deatils of book to be inserted:";
b1.getd();
f2.write((char*)&b1,sizeof(book));
f2.write((char*)&obj,sizeof(book));
i++;
}
}
f1.close();
f2.close();
rewrite();
cout<<"\nNew Record inserted at position "<<n;
}
void insid()
{
book b1;
cout<<"\nEnter Book ID of position:";
cin>>n;
f1.open("Data.dat",ios::in|ios::binary);

f2.open("temp.dat",ios::out|ios::binary);
while(f1.read((char*)&obj,sizeof(book)))
{
if(obj.retid()!=n)
{
f2.write((char*)&obj,sizeof(book));
i++;
}
else
{
f2.write((char*)&obj,sizeof(book));
cout<<"\nEnter details of new record:";
b1.getd();
f2.write((char*)&b1,sizeof(book));
i++;
}
}
f1.close();
f2.close();
rewrite();
cout<<"\nNew record inserted after book id "<<n;
}
void searchid()
{
int flag=0;
cout<<"\nEnter the ID to be searched:";
cin>>n;
f1.open("Data.dat",ios::in|ios::binary);
while(f1.read((char*)&obj,sizeof(book)))
{
if(obj.retid()==n)
{
obj.display();
flag=1;
}
}
if(flag==0)
cout<<"\nThe book-id was not found";
f1.close();
}
void searchnm()
{
char nm[20];
int flag=0;
cout<<"\nEnter the name to be searched:";
gets(nm);
f1.open("Data.dat",ios::in|ios::binary);
while(f1.read((char*)&obj,sizeof(book)))
{
if(strcmp(nm,obj.retname())==0)
{
obj.display();
flag=1;
}
}
if(flag==0)

cout<<"\nThe name was not found";


f1.close();
}
void count()
{
n=0;
f1.open("Data.dat",ios::in|ios::binary);
while(f1.read((char*)&obj,sizeof(book)))
n++;
cout<<"\nThe number of records="<<n;
f1.close();
}
void main()
{
int choice;
char reply;
clrscr();
cout<<"Menu:";
do
{
cout<<"\n1.Create\n2.Append\n3.Read\n4.Edit nth record\n5.Delete nth record\n";
cout<<"6.Insert nth record\n7.Insert after ID\n8.Search by ID\n9.Search by Name";
cout<<"\n10.Count number of records\nEnter Your choice:";
cin>>choice;
switch(choice)
{
case 1:create();
break;
case 2:append();
break;
case 3:read();
break;
case 4:edit();
break;
case 5:delrec();
break;
case 6:insrec();
break;
case 7:insid();
break;
case 8:searchid();
break;
case 9:searchnm();
break;
case 10:count();
break;
default:cout<<"\nYou have entered a wrong choice";
}
cout<<"\nDo you wish to perfrom another action?(y/n):";
cin>>reply;
}while(reply=='y'||reply=='Y');
getch();
}
//OUTPUT
Menu:
1.Create

2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:1
Enter book id:1
Enter name of book:Maths
Enter number of pages:420
Do you wish to enter more?(y/n):y
Enter book id:2
Enter name of book:Physics
Enter number of pages:210
Do you wish to enter more?(y/n):y
Enter book id:3
Enter name of book:Computer Science
Enter number of pages:220
Do you wish to enter more?(y/n):n
File Successfully Created
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:2
Enter book id:4
Enter name of book:English
Enter number of pages:12
Do you wish to enter more?(y/n):n
File successfully Appended
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID

9.Search by Name
10.Count number of records
Enter Your choice:3
Contents of the file:
Book ID:1
Name:Maths
Pages=420
Book ID:2
Name:Physics
Pages=210
Book ID:3
Name:Computer Science
Pages=220
Book ID:4
Name:English
Pages=12
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:4
Enter the record number to be edited:4
Enter the Details
Enter book id:4
Enter name of book:Chemistry
Enter number of pages:310
Record number 4 edited successfully
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:3

Contents of the file:


Book ID:1
Name:Maths
Pages=420
Book ID:2
Name:Physics
Pages=210
Book ID:3
Name:Computer Science
Pages=220
Book ID:4
Name:Chemistry
Pages=310
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:5
Enter the record number to be deleted:2
Record number 2 deleted from file
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:3
Contents of the file:
Book ID:1
Name:Maths
Pages=420
Book ID:3

Name:Computer Science
Pages=220
Book ID:4
Name:Chemistry
Pages=310
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:6
Enter the position to enter record:2
Enter the deatils of book to be inserted:
Enter book id:42
Enter name of book:English
Enter number of pages:12
New Record inserted at position 2
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:3
Contents of the file:
Book ID:1
Name:Maths
Pages=420
Book ID:42
Name:English
Pages=12
Book ID:3
Name:Computer Science
Pages=220
Book ID:4

Name:Chemistry
Pages=310
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:7
Enter Book ID of position:42
Enter details of new record:
Enter book id:5
Enter name of book:Computer Science
Enter number of pages:540
New record inserted after book id 42
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:3
Contents of the file:
Book ID:1
Name:Maths
Pages=420
Book ID:42
Name:English
Pages=12
Book ID:5
Name:Computer Science
Pages=540
Book ID:3
Name:Computer Science
Pages=220

Book ID:4
Name:Chemistry
Pages=310
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:8
Enter the ID to be searched:42
Book ID:42
Name:English
Pages=12
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:9
Enter the name to be searched:English
Book ID:42
Name:English
Pages=12
Do you wish to perfrom another action?(y/n):y
1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:10
The number of records=5

Do you wish to perfrom another action?(y/n):y


1.Create
2.Append
3.Read
4.Edit nth record
5.Delete nth record
6.Insert nth record
7.Insert after ID
8.Search by ID
9.Search by Name
10.Count number of records
Enter Your choice:11
You have entered a wrong choice
Do you wish to perfrom another action?(y/n):n

Вам также может понравиться