Program 7 – Binary File Implementation

Write a program that creates a binary file by reading the data for the students from the terminal. The data of each student consists of roll no., name ( a string of 30 or lesser no. of characters) and marks.

#include<iostream>
#include<fstream>
using namespace std;

class student{
    int rn, marks;
    char name[30];
    public:
    void getData()
    {
        cout << "Enter name: ";
        cin >> name;
        cout << "Enter roll no: ";
        cin >> rn;
        cout << "Enter marks: ";
        cin >> marks;
    }
};

int main()
{
    int m;
    fstream f;
    f.open("Student.txt",ios::binary|ios::out);
    cout << "Enter number of students: ";
    cin >> m;
    student st[m];
    for(int i=0;i<m;i++)
    {
        st[i].getData();
        f.write((char *)&st[i], sizeof(st[i]));
    }
    f.close();
    return 0;
}