C++ Language



Admission Enquiry Form

  

Write Data into the File Using Class

Write a code to accept name,rollno,fee from the user and write them into the file, Using Array of an Object.

//file handling in cpp
#include<iostream>
#include<fstream>
using namespace std;
class Student
{
private:
    int rollno;
    char name[15];
    float fee;
    ofstream fout;
    fstream file;
public:
    Student()//default constructor
    {
        file.open("abc.txt",ios::out);
    }//end of constructor
    Student(int rollno,float fee,char name[])//signature of the fun.
    {

    }
void setData()
{
cout<<"Enter name of the student";
cin>>name;
cout<<"Enter rollno of the student";
cin>>rollno;
cout<<"Enter fee of the student";
cin>>fee;
}//end of setData
void getData()
{
cout<<endl<<"Name is :"<<name;
cout<<endl<<" Rollno is :"<<rollno;
cout<<"\nFee is :"<<fee;
}
void saveData()
{
fout.open("c:\\ajit\\abc.txt");
fout<<name<<" "<<rollno<<" "<<fee;
fout.close();
cout<<"\n Data saved into the file";
}//end of function
};
int main()
{
cout<<"using file handling";
Student st[5];
st[0].setData();
st[0].getData();
st[0].saveData();
}//end of main