9.1.
Program to implement text file
Program:
#include<fstream>
#include<iostream>
#include<string.h>
using namespace std;
void create()
{
int n,i=0;
char A[80];
ofstream f1("Sample.txt");
if(!f1)
{
cout<<"\nERROR!!";
return;
}
cout<<"\nEnter the number of lines";
cin>>n;
do
{
cout<<"\nEnter a line";
cin.getline(A,80);
f1<<A;
f1<<"\n";
i++;
}
while(i<=n);
f1.close();
cout<<"\nFile created!!";
}
void display()
{
char ch[80],A[80];
ifstream f1("Sample.txt");
ifstream f2("New.txt");
cout<<"\nContents of file 1";
while(f1.eof()==0)
{
f1.getline(A,80);
cout<<"\n"<<A;
}
while(f2.eof()==0)
{
f2.getline(ch,80);
cout<<ch;
cout<<"\n";
}
f1.close();
f2.close();
}
int main()
{
int i=0;
char a;
create();
ifstream f1("Sample.txt");
ofstream f2("New.txt");
while(f1.eof()==0)
{
f1.get(a);
if(a==' ')
i++;
else
{
f2<<a;
i++;
}
}
cout<<"\nContents copied!!";
f1.close();
f2.close();
display();
return(0);
}
output::
[4090@localhost ~]$ g++ ex91.cpp
[4090@localhost ~]$ ./a.out
Enter the number of lines 3
Enter a line
Enter a linefirst line
Enter a line second line
Enter a line third line
File created!!
Contents copied!!
Contents of file 1
first line
 second line
 third line
firstline
secondline
thirdline
9.2.Program to implement binary file
Program:
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;
class employee
{
int id;
char name[20],designation[20],quali[20];
float salary;
public:
employee()
{
id=0;
strcpy(name," ");
strcpy(designation," ");
strcpy(quali," ");
salary=0.0;
}
void getdata()
{
cout<<"\nEnter the employee details\nID:";
cin>>id;
cout<<"\nName:";
cin>>name;
cout<<"\nDesignation:";
cin>>designation;
cout<<"\nQualification:";
cin>>quali;
cout<<"\nSalary:";
cin>>salary;
}
void display()
{
cout<<"\nEmployee
details:\nID:"<<id<<"\nName:"<<name<<"\nDesignation:"<<designation<<"\n
Qualification:"<<quali<<"\nSalary:"<<salary;
}
int ret_id()
{
return id;
}
}E;
void create(int n)
{
int i;
ofstream f1("employee.dat");
if(!f1)
{
cout<<"\nError!!!!";
return;
}
for(i=0;i<n;++i)
{
E.getdata();
f1.write((char*)&E,sizeof(E));
}
f1.close();
cout<<"\nFile created!!";
}
int main()
{
int opt,n,id,flag=0;
do
{
cout<<"\n\n1.Create a binary file\n2.Display all employee details\n3.Display a
particular employee's details\n4.Update a record\n5.Delete a record\n6.To print
the three records\n7.Exit\nChoose option:";
cin>>opt;
if(opt==1){
       cout<<"\nEnter the number of employee details:";
       cin>>n;
       create(n);
       }
if(opt==2)
       {
       ifstream f1("employee.dat");
       cout<<"\nEmployee details:";
       while(f1.read((char*)&E,sizeof(E)))
       {
      E.display();
      }
      f1.close();
      }
if(opt==3)
       {
       cout<<"\nEnter the id:";
       cin>>id;
       ifstream f2("employee.dat");
       while(f2.read((char*)&E,sizeof(E)))
       {
       if(E.ret_id()==id)
       {
       E.display();
       flag=1;
       }
       }
       if(flag==0)
       cout<<"\nEmployee not found!!";
       f2.close();
       }
if(opt==4)
       {
       cout<<"\nEnter the ID of the record you want to update:";
       cin>>id;
       ifstream f3("employee.dat");
       ofstream f4("temp.dat");
       if(!f3 && !f4)
       {
       cout<<"\nError!!";
       break;
       }
       while(f3.read((char*)&E,sizeof(E)))
       {
       if(E.ret_id()==id)
       {
       E.getdata();
       f4.write((char*)&E,sizeof(E));
       }
       else
       f4.write((char*)&E,sizeof(E));
      }
      f3.close();
      f4.close();
      remove("employee.dat");
      rename("temp.dat","employee.dat");
      }
if(opt==5)
        {
        cout<<"\nEnter the ID of the record that you want to delete:";
        cin>>id;
        ifstream f5("employee.dat");
        ofstream f6("temp.dat");
        while(f5.read((char*)&E,sizeof(E)))
        {
        if(E.ret_id()!=id)
        {
        f6.write((char*)&E,sizeof(E));
        }
        }
        f5.close();
        f6.close();
        remove("employee.dat");
        rename("temp.dat","employee.dat");
        }
if(opt==6)
       {
       int i;
       cout<<"\nEnter the ID of the person:";
       cin>>id;
       ifstream f7("employee.dat");
       if(!f7)
       {
       cout<<"\nERRORR!!!!";
       break;
       }
       while(f7.read((char*)&E,sizeof(E)))
       {
       if(E.ret_id()==id)
        {
               for(i=0;i<3;++i)
               {
               f7.read((char*)&E,sizeof(E));
              E.display();
              }
         }
        }
        f7.close();
    }
}
while(opt!=7);
return(0);
output::
[4090@localhost ~]$ g++ ex92.cpp
[4090@localhost ~]$ ./a.out
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:1
Enter the number of employee details:5
Enter the employee details
ID:1
Name:samantika
Designation:ceo
Qualification:ceo
Salary:1000
Enter the employee details
ID:2
Name:sarah
Designation:ceo
Qualification:ceo
Salary:1000
Enter the employee details
ID:3
Name:priya
Designation:ceo
Qualification:cep
Salary:2000
Enter the employee details
ID:4
Name:santosh
Designation:ceo
Qualification:ceo
Salary:2000
Enter the employee details
ID:5
Name:nitish
Designation:ceo
Qualification:ceo
Salary:3000
File created!!
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:2
Employee details:
Employee details:
ID:1
Name:samantika
Designation:ceo
Qualification:ceo
Salary:1000
Employee details:
ID:2
Name:sarah
Designation:ceo
Qualification:ceo
Salary:1000
Employee details:
ID:3
Name:priya
Designation:ceo
Qualification:cep
Salary:2000
Employee details:
ID:4
Name:santosh
Designation:ceo
Qualification:ceo
Salary:2000
Employee details:
ID:5
Name:nitish
Designation:ceo
Qualification:ceo
Salary:3000
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:3
Enter the id:3
Employee details:
ID:3
Name:priya
Designation:ceo
Qualification:cep
Salary:2000
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:4
Enter the ID of the record you want to update:4
Enter the employee details
ID:4
Name:santosh
Designation:clerk
Qualification:becse
Salary:10
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:2
Employee details:
Employee details:
ID:1
Name:samantika
Designation:ceo
Qualification:ceo
Salary:1000
Employee details:
ID:2
Name:sarah
Designation:ceo
Qualification:ceo
Salary:1000
Employee details:
ID:3
Name:priya
Designation:ceo
Qualification:cep
Salary:2000
Employee details:
ID:4
Name:santosh
Designation:clerk
Qualification:becse
Salary:10
Employee details:
ID:5
Name:nitish
Designation:ceo
Qualification:ceo
Salary:3000
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:5
Enter the ID of the record that you want to delete:4
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:2
Employee details:
Employee details:
ID:1
Name:samantika
Designation:ceo
Qualification:ceo
Salary:1000
Employee details:
ID:2
Name:sarah
Designation:ceo
Qualification:ceo
Salary:1000
Employee details:
ID:3
Name:priya
Designation:ceo
Qualification:cep
Salary:2000
Employee details:
ID:5
Name:nitish
Designation:ceo
Qualification:ceo
Salary:3000
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:6
Enter the ID of the person:1
Employee details:
ID:2
Name:sarah
Designation:ceo
Qualification:ceo
Salary:1000
Employee details:
ID:3
Name:priya
Designation:ceo
Qualification:cep
Salary:2000
Employee details:
ID:5
Name:nitish
Designation:ceo
Qualification:ceo
Salary:3000
1.Create a binary file
2.Display all employee details
3.Display a particular employee's details
4.Update a record
5.Delete a record
6.To print the three records
7.Exit
Choose option:7
[4090@localhost ~]$ g++ ex10.1.cpp
[4090@localhost ~]$ ./a.out
Enter no. of accounts:3
Account 1
Account number:1
Account name:samantika
Current balance:10000
Creation date:2oct2017
Phone no:999999
Account 2
Account number:2
Account name:samantika
Current balance:20000
Creation date:3sept2017
Phone no:888888
Account 3
Account number:3
Account name:sarah
Current balance:20000
Creation date:5oct2017
Phone no:77777
1.Display
2.Sort
3.Insert
4.Exit
Enter choice:1
Displaying all account details
Position 1
Account number:1
Account name:samantika
Current balance:10000
Creation date:2oct2017
Phone no:999999
Position 2
Account number:2
Account name:samantika
Current balance:20000
Creation date:3sept2017
Phone no:888888
Position 3
Account number:3
Account name:sarah
Current balance:20000
Creation date:5oct2017
Phone no:77777
1.Display
2.Sort
3.Insert
4.Exit
Enter choice:2
Displaying all account details
Position 1
Account number:1
Account name:samantika
Current balance:10000
Creation date:2oct2017
Phone no:999999
Position 2
Account number:2
Account name:samantika
Current balance:20000
Creation date:3sept2017
Phone no:888888
Position 3
Account number:3
Account name:sarah
Current balance:20000
Creation date:5oct2017
Phone no:77777
1.Display
2.Sort
3.Insert
4.Exit
Enter choice:3
Enter the position:2
Enter account details:Account number:44
Account name:nitiiii
Current balance:500000
Creation date:5oct2017
Phone no:88888
Account insertedPosition 1
Account number:1
Account name:samantika
Current balance:10000
Creation date:2oct2017
Phone no:999999
Position 2
Account number:44
Account name:nitiiii
Current balance:500000
Creation date:5oct2017
Phone no:88888
Position 3
Account number:2
Account name:samantika
Current balance:20000
Creation date:3sept2017
Phone no:888888
Position 4
Account number:3
Account name:sarah
Current balance:20000
Creation date:5oct2017
Phone no:77777
1.Display
2.Sort
3.Insert
4.Exit
Enter choice:1
Displaying all account details
Position 1
Account number:1
Account name:samantika
Current balance:10000
Creation date:2oct2017
Phone no:999999
Position 2
Account number:44
Account name:nitish
Current balance:500000
Creation date:5oct2017
Phone no:88888
Position 3
Account number:2
Account name:samantika
Current balance:20000
Creation date:3sept2017
Phone no:888888
Position 4
Account number:3
Account name:sarah
Current balance:20000
Creation date:5oct2017
Phone no:77777
1.Display
2.Sort
3.Insert
4.Exit
Enter choice:4
[4090@localhost ~]$