C++ Input & Output

The input  operation requires the creation of an input stream and linking it with the program and input the file. Output operation requires creation of an output stream and linking it with the program and the output file.

 

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

class emp
{
int emp_numb;
char name[30];
public :
void getdata()
{
cout<<“Enter employee number\n”;
cin>>emp_numb;
cout<<“\n Enter name\n”;
cin>>name;
}
void showdata()
{
cout<<“Employee number:”<<emp_numb;
cout<<“\n Name : “<<name;
}
};
void main()
{
clrscr();
emp ob1,ob2;
ob1.getdata(); // get data for employee ob1
ofstream file_out(“test.txt”); //create file for output
file_out.write((char *)&ob1, sizeof(ob1)); //write ob1 to the file
file_out.close(); // close the file
ifstream file_in(“test.txt”);
file_in.read((char *)&ob2, sizeof(ob2)); // read file into object ob2
ob2.showdata();
getch();
}

Result :- 

input-in-a-file-in-c++

 




Follow Us

Contact Us

tutorialworldin@yahoo.com

Back to Top