C++ program to store records of an Object of Class to a FILE

//program to store record of an employee to a file named emp.txt
//class Employee with id,name,address and salary as data members

#include <iostream>
#include <fstream>
using namespace std;
class Employee
{
    public:
        int id;
        char name[20],address[20];
        float salary;
        void readRecord()
        {
            cout<<"Enter ID: ";
            cin>>id;
            cout<<"Enter name: ";
            cin>>name;
            cout<<"Enter address: ";
            cin>>address;
            cout<<"Enter salary: ";
            cin>>salary;
        }
};
int main()
{
    Employee e;
    e.readRecord();
    ofstream out("emp.txt");
    cout<<"Writing to a file.....";
    out<<e.id<<endl<<e.name<<endl<<e.address<<endl<<e.salary;
    out.close();
    return 0;
}

No comments:

Post a Comment