Tuesday 23 April 2013

FILE HANDLING PROGRAMS

Description:

Here some tasks are performed using file handling concept.

CODE1:


#include<conio.h>
#include<fstream>
using namespace std;
int main()
{
ofstream outfile("TEST.TXT");
outfile<<"Ifear the ancient thee,ancient Mariner!\n";
outfile<<"I fear the skinny hand\n";
    outfile<<"And thou art long, and lank, and brown\n;
    outfile<<"As is the ribbed sea sand.\n";
getche();
}


CODE2:


#include<fstream>
#include<conio.h>
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str="Time is a great teacher, but unfortunately"
"it kills all its pupils. Berlioz";
ofstream outfile("TEST.TXT");
for(intj=0;j<str.size();j++)
outfile.put(str[j]);
cout<<"File written\n"
getche();
}


CODE3:

#include<fstream>
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
ifstream infile("TEST.TXT");
cout<<infile.rdbuf();
cout<<endl;
getche();
}

CODE4:

#include<fstream>
#include<conio.h>
#include<iostream>
using namespace std;
class person
{
protected:
char name [80];
short age;
public:
void getData()
{
cout<<"Enter name : "cin>>name;
cout<<"Enter age: "cin>>age;
}
};
int main()
{
person pers;
pers.getData();
ofstream outfile("PERSON.DAT", ios::binary);
outfile.write(reinterpert_cast<char*>(&pers),sizeof(pers));
getche();
}

CODE5:

#include<fstream>
#include<conio.h>
#include<iostream>
class person
{
protected:
char name[80];
int age;
public:
void getdata()
{
cout "\nEnter name:";cin>>name;
cout<<"Enter age: ";cin >>age;
}
void showdata()
{
cout<<"\n Name: "<<name;
cout<<"\n Age: "<<age;
}
};
int main()
{
char ch;
person pers;
fstream file;
file.open("GROUP.DAT"), ios::app|ios::out|
ios::in|ios::binary);
do
{
cout<<"\nEnter person's data:";
pers.getdata();
file.write(reinterpret_cast<char*>(&pers),
sizeof(pers));
cout <<"enter another person(y/n)?"
cin>>ch;
}


No comments:

Post a Comment