Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

/*

Name : Ranjit jeve


Roll no - 24
Pract no: 08
Group: A(1)
Title: Demonstrate various file operations using C++.
Objectives: 1) To learn and understand streams and files in object oriented paradigm.
2) To demonstrate file operations like create, open, read, write and close a file.
Problem Statement: Write a C++ program that creates an output file, writes information to it,
closes the file and open it again as an input file and read the information from the file.
*/
#include<iostream>
#include<fstream>
using namespace std;
class file{
char name[20];
int emp_id;
float salary;
public:
void accept()
{
cin>>name;
cin>>emp_id;
cin>>salary;
}
void display()
{
cout<<"\n"<<name<<"\t"<<emp_id<<"\t"<<salary;
}
};
int main()
{
file o[5];
fstream f;
int i,n;
f.open("input1.txt");
cout<<"\n How many employee information wanted to store";
cin>>n;
cout<<"\n Enter information of employees(Enter name,emp_id,salary)";
for(i=0;i<n;i++)
{
cout<<"\n enter information of" <<i<< "employee:\n ";
o[i].accept(); //accept input foem user
f.write((char*) &o[i],sizeof(o[i])); //write object in file
}
f.close();
f.open("input",ios::in);
cout<<"\n Information of employee is as follows";
for(i=0;i<n;i++)
{
f.read((char*) &o[i],sizeof(o[i])); //read data form file
o[i].display(); //display data
}
f.close();
return 0;
}
/*
How many employee information wanted to store3

Enter information of employees(Enter name,emp_id,salary)


enter information of0employee:
ms
123
12000

enter information of1employee:


sc
234
11000

enter information of2employee:


sm
124
10000

Information of employee is as follows


ms 123 12000
sc 234 11000
sm 124 10000[student@localhost ~]$

*/

You might also like