Professional Documents
Culture Documents
Binary (10pt)
Binary (10pt)
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)
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
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