FILE HANDLING IN C++
CLASS AND FILE OPERATION:-
#include<fstream>
#include<iostream>
using namespace
std;
class
student_info
{
protected:
char name[20];
int age;
char sex;
float height;
float weight;
public:
void getdata();
void display();
};
void student_info::
getdata()
{
cout<<"enter yur name
";
cout <<" name :";
cin>> name ;
cout<<"enter yur
age ";
cout <<" age :";
cin>> age ;
cout<<"enter yur
sex ";
cout <<" sex :";
cin>> sex ;
cout<<"enter yur
height ";
cout <<" height :";
cin>> height ;
cout<<"enter yur
weight ";
cout <<" weight :";
cin>> weight ;
}
void
student_info::display()
{
cout<< name<< age<< sex
<< height<< weight<< endl;
}
int main()
{
student_info o;
fstream infile;
char fname[10];
cout<<"enter a file name";
cin>> fname;
infile.open(fname, ios :: in | ios :: out
);
o.getdata();
infile.open(fname,ios::out);
cout<<"storing a file";
infile.write((char*) &o,sizeof(o));
infile.close();
// reading file frm a file
infile.open(fname,ios::in);
cout<<"reading frm a
file..............";
infile.read((char*) &o,sizeof(o));
o.display();
infile.close();
return 0;
}
MERGE TWO FILES INTO ONE FILE:-
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace
std;
class emp
{
int num,age;
char name[20],dep[5];
public:
void getdata()
{
cout<<"\n\n Name
= ";
cin>>name;
cout<<"\n Emp Num = ";
cin>>num;
cout<<"\n Department=
";
cin>>dep;
cout<<"\n Age = ";
cin>>age;
}
void display1()
{
cout<<"\n"<<name<<"\t"<<num<<"\t"<<dep<<"\t\t"<<age;
}
};
class sal
{
float gs,ns;
public:
void getsal()
{
cout<<"\n Gross sal =
";
cin>>gs;
cout<<"\n Net sal = ";
cin>>ns;
}
void display2()
{
cout<<"\t"<<gs<<"\t"<<ns;
}
};
void display()
{
emp e;sal s;
ifstream fil1;
fil1.open("empdetails.txt",ios::in);
cout<<"\n\n Name \t Emp Num \t
Dep \t Age \t Gross Sal \t Net Sal \n";
while(!fil1.eof())
{
fil1.read((char*)&e,sizeof(e));
e.display1();
fil1.read((char*)&s,sizeof(s));
s.display2();
}
}
int main()
{
int n;
emp e1;sal s1;
ofstream fil1,fil2,fil3;
fil1.open("emp.txt",ios::out);
fil2.open("sal.txt",ios::out);
fil3.open("empdetails.txt",ios::out);
cout<<"\n How many employee
details do you want to enter = ";
cin>>n;
cout<<"\n Enter the deatils one
by one \n";
for(int i=0;i<n;i++)
{
e1.getdata();
fil1.write((char*)&e1,sizeof(e1));
s1.getsal();
fil2.write((char*)&s1,sizeof(s1));
fil3.write((char*)&e1,sizeof(e1));
fil3.write((char*)&s1,sizeof(s1));
}
fil1.close();
fil2.close();
fil3.close();
cout<<"\n\n\t\t Merged file
contents \n\n\t\t";
display();
getch();
return 0;
}
OPEN A BINARY FILE:-
#include<fstream>
#include<iostream>
using namespace
std;
int main()
{
ofstream outfile;
char fname[10];
float x,y,temp;
cout<<" enter a file
name";
cin>>fname;
outfile.open(fname ,ios::out |
ios::binary);
x=16.5;
y=10.5;
cout<<"x ............ temp"<< endl;
while(x<=y)
{
temp=x*x;
outfile<<
x<<'/t'<< temp<< endl;
cout<< x << '/t'<< temp<< endl;
x=x+1.5;
}
outfile.close();
return 0;
}
PROGRAMME Q1:-
#include<fstream>
using namespace
std;
int main()
{
ifstream infile;
infile.open("data.txt");
cout<<"file has to be
opened";
while((!infile.eof()))
{
ch=infile.get();
cout.put(ch);
}
infile.close();
return 0;
}
RANDOM ACCESS:-
#include<iostream>
#include<fstream>
using namespace
std;
int main()
{
fstream infile;
char fname[20];
char ch;
cout<<"enter a name of the
file";
cin >> fname;
infile.open(fname ,ios::in | ios::out);
infile.seekg(5l,ios::beg);
infile.get(ch);
cout<<" after 5 chr
begins........."<<ch;
cout<<"/n";
infile.seekg(-10l,ios::end);
infile.get(ch);
cout<<" after 10 chr
begins........."<<ch;
cout<<"/n";
infile.seekg(0,ios::cur);
infile.get(ch);
cout<<" chr begins........."<<ch;
cout<<"/n";
infile.close();
return 0;
}
READ LINES FROM A KEYBOARD:-
// reading a text
frm a file and store in a specific file
#include<iostream>
#include<fstream>
using namespace
std;
const int MAX =
2000;
int main()
{
ofstream outfile;
char fname[10],line[MAX];
cout<<"enter a file name to
opened";
cin>>fname;
outfile.open(fname);
cout<<"enter a set of specific
lines end with @\n";
cin.get(line,MAX,'@');
cout<<"given input \n";
cin>>line;
cout<<"storing in a file";
outfile<<line;
outfile.close();
return 0;
}
STRUCTURE AND FILE OPERATION:-
#include<fstream>
#include<iostream>
using namespace
std;
const int MAX
=200;
struct school
{
char name[20];
int age;
char sex;
float height;
float weight;
};
int main()
{
struct school student[MAX];
fstream infile;
char fname[10];
int i,n;
cout<<"enter a file name";
cin>> fname;
infile.open(fname, ios :: in | ios :: out
);
cout<<" how mny record to store";
cin>>n;
cout<<"enter the following
info";
for(i=0;i<=n-1;++i)
{
cout<<"enter yur name
";
cout <<" name :";
cin>>student[i].name ;
cout<<"enter yur
age ";
cout <<" age :";
cin>> student[i].age ;
cout<<"enter yur
sex ";
cout <<" sex :";
cin>>student[i].sex ;
cout<<"enter yur
height ";
cout <<" height :";
cin>>student[i].height ;
cout<<"enter yur
weight ";
cout <<" weight :";
cin>> student[i].weight ;
}
infile.open(fname,ios::out);
cout<<"storing a file";
for(i=0;i<=n-1;++i)
{
infile.write((char*)
&student[i],sizeof(student[i]));
}
infile.close();
infile.open(fname,ios::in);
cout<<"reading frm a
file..............";
for(i=0;i<=n-1;++i)
{
infile.write((char*)
&student[i],sizeof(student[i]));
cout <<student[i].name
<<student[i].age <<student[i].height <<student[i].weight<<student[i].height
;
}
infile.close();
return 0;
}
PROGRAMME:-
#include<fstream>
#include<iostream>
using namespace
std;
int main()
{
ofstream outfile;
char fname[10];
float x,y,temp;
cout<<" enter a file
name";
cin>>fname;
outfile.open(fname ,ios::out |
ios::binary);
x=16.5;
y=10.5;
cout<<"x ............ temp"<< endl;
while(x<=y)
{
temp=x*x;
outfile<<
x<<'/t'<< temp<< endl;
cout<< x << '/t'<< temp<< endl;
x=x+1.5;
}
outfile.close();
return 0;
}
WRITE LINE IN A FILE:-
//storing ia
textt in a file
#include<iostream>
#include<fstream>
using namespace
std;
int main()
{
ofstream outfile;
outfile.open("text.dat");
outfile<<"this is a test
program";
outfile.close();
return 0;
}
WRITE TO A USER-DEFINED FILE:-
#include<iostream>
#include<fstream>
using namespace
std;
int main()
{
ofstream outfile;
char fname[10];
cout<<"enter a name to open a
file";
cin>>fname;
outfile.open(fname);
outfile<<"this is a test
program";
outfile<< "program to store a
file";
outfile<<" a set of lines to
store in a file";
outfile.close();
return 0;
}
FILE HANDLING IN C++
Reviewed by Marketing Thrills
on
September 16, 2015
Rating:
No comments:
Post a Comment