Sunday, March 17, 2013

Class With Files

#include<iostream>
#include<fstream.h>//file stream
using namespace std;
class student{
                 char name[30];
                 int age;
                 public:
                    void setdata();
                    void display();
             };
typedef student su;
void su::setdata()
{
    ofstream fil("su.txt");//opening file for writing
    cout<<"Enter Name: ";
    cin.getline(name,28);
    cout<<"Enter Age: ";
    cin>>age;
    fil<<name;
    fil<<age;
    fil.close();
}
void su::display()
{
    ifstream fil_1("su.txt");//opening file for reading
    fil_1>>name;
    fil_1>>age;
    cout<<"\n\nName: "<<name;
    cout<<"\nAge: "<<age;
}
int main()
{
    su a;//declaring class objects
    a.setdata();
    a.display();
    return 0;
}

0 comments:

Post a Comment