Sunday, March 20, 2022

binary file handling by tarun

 #include <iostream>

#include <fstream>
using namespace std;
class student
{
public:
    int roll;
    string name;
    void getdata()
    {
        cout << "ENTER ROLL NO : " << endl;
        cin >> roll;
        cout << "ENTER NAME : " << endl;
        cin >> name;
    }
    void display()
    {
        cout << "YOUR ROLL NUMBER IS : " << roll << endl;
        cout << "YOUR NAME IS : " << name << endl;
    }
};
int main()
{
    student obj;
    ofstream binfile;
    binfile.open("bin.dat", ios::binary); // opened file in binary mode
    for (int i = 0; i < 3; i++)
    {
        obj.getdata();
        binfile.write((char *)&obj, sizeof(obj)); // writing data in binary file taking the reference of the data stored in
        // object of student class
    }

    binfile.close();

    // now for showing the data that is stored in binaryfile we create ifstream
    ifstream binfile2;
    binfile2.open("bin.dat", ios::binary);

    while (binfile2.read((char *)&obj, sizeof(obj))) // while read() is reading from the binaryfile it is true

    {
        obj.display();
    }

    binfile2.close();

    return 0;
}

No comments:

Software scope

 In software engineering, the software scope refers to the boundaries and limitations of a software project. It defines what the software wi...

Popular Posts