'How to read/write doubles in a binary file?

I am creating a program that will write and read the content of a binary file. The part I am struggling with is reading/writing pointsEarned and studentGPA as a double. It prints out 6.36599e-314 instead of the number I provided. I looked around the web for a solution but couldn't find one, so here I am.

    class student {
        int studentID;
        char name[30];
        char lastName[30];
        double pointsEarned;
        double studentGPA;

    public:
        void getData() {
            cout << "\nEnter the Student ID: ";
            cin >> studentID;
            cout << "\n\nEnter the first name of the student: ";
            cin >> name;
            cout << "\n\nEnter the last name of the student: ";
            cin >> lastName;
            cout << "\n\nEnter the amount of points earned by the student: ";
            cin >> pointsEarned;
            cout << "\n\nEnter the Student GPA: ";
            cin >> studentGPA;

    }


    void showData() {
        cout << "\nStudent ID: " << studentID;
        cout << "\nStudent Name: " << name << " " << lastName;
        cout << "\nPoints: " << pointsEarned;
        cout << "\nGPA: " << studentGPA << "\n";
    }

    int studentNumber() {
        return studentID;
    }

    };

    //input student info into the dat file
        void write() {
        student object;

        student val;
        ofstream file2;

        file2.open("student.dat", ios::binary | ios::app);
        object.getData();

        file2.write((char *)&object, sizeof(object));
        file2.close();

    }

void display() {
    student object;
    ifstream file1;
    file1.open("student.dat", ios::binary);

    if (!file1) {
        cout << "File not Found!\n";
    }
    else {
        //read data
        while (file1.read((char *)&object, sizeof(object))) {
            object.showData();
        }
    }
    file1.close();
}

//display student information through their studentID
void search(int n) {
    student object;
    ifstream file1;

    file1.open("student.dat", ios::binary);
    if (!file1) {
        cout << "File not Found!\n";
    }
    else {
        while (file1.read((char *)&object, sizeof(object))) {
            if (object.studentNumber() == n)
                object.showData();
            //validation
            if (object.studentNumber() != n) {
                cout << "StudentID doesn't exist\n";
                break;
            }
        }
    }

    file1.close();

}

//delete student

void deleteRecord(int n) {
    student object;
    //open both files
    ifstream file1;
    file1.open("student.dat", ios::binary);
    ofstream file2;
    file2.open("Temp.dat", ios::out|ios::binary);

    //write contents of f1 to f2
    while (file1.read((char*)&object, sizeof(object))) {
        if (object.studentNumber() != n)
            file2.write((char*)&object, sizeof(object));
    }
    file1.close();
    file2.close();
    //delete original file and rename the new one
    remove("student.dat");
    rename("Temp.dat", "student.dat");
}

void editRecord(int n) {
    fstream fp;
    student object;
    int found = 0;
    fp.open("student.dat", ios::in | ios::out);

    while (fp.read((char *)&object, sizeof(object)) && found == 0) {
        if (object.studentNumber() == n) {
            object.showData();
            cout << "\nEnter The New Details of student";
            object.getData();
            int pos = -1 * sizeof(object);
            fp.seekp(pos, ios::cur);
            fp.write((char*)&object, sizeof(object));
            found = 1;
        }

        //validation
        if (object.studentNumber() != n) {
            cout << "StudentID doesn't exist\n";
            break;
        }
    }
    fp.close();
}

    int main() {
        int n;
        int studentIDNumber;
            cout << " Press 1 if you choose to input student information\n Press 2 to display all student's information\n Press 3 to display a specific student's information\n Press 4 to modify student information\n Press 5 to delete a student\n";
        cin >> n;

    if (n == 1) {
        write();
    } else if(n == 2){
        display();
    }
    else if (n == 3) {
        cout << "Enter the Student ID to get the student information: ";
        cin >> studentIDNumber;
        search(studentIDNumber);
    }
    else if (n == 4) {
        cout << "Enter the Student ID to modify the student information: ";
        cin >> studentIDNumber;
        editRecord(studentIDNumber);
    }
    else if (n == 5) {
        deleteRecord(studentIDNumber);
        cout << "The record has been deleted.\n";
    }
    else {
        cout << "Error. The number is neither 1 or 2. Please try again. \n";
    }
    return 0;

}

    The output: 

    Student ID: 55
    Student Name: John Doe
    Points: 6.36599e-314
    GPA: 7.27464e+199
    Press any key to continue . . .
c++


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source