Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- using namespace std;
- struct Student {
- int kcstID;
- int quiz1;
- int quiz2;
- int quiz3;
- float average;
- };
- int main() {
- // Write Code to open Binary File
- ofstream exam ("student.dat" , ios::out | ios::binary);
- const int n=5 ;
- if(!exam) {
- cout << "Cannot open file" << endl;
- return 1;
- }
- Student stu[n] = { {163443, 8, 8, 9},
- {165143,5,4,6},
- {185421,10,8,9},
- {165841,9,8,10},
- {162315,7,8,8} };
- for ( int i = 0 ; i < n ; i++){
- stu[i].average = (stu[i].quiz1 + stu[i].quiz2 + stu[i].quiz3) / 3.0 ;
- exam.write((char *)&stu[i], sizeof(Student));
- }
- // Write Code to write to file using structure in Binary format
- if (!exam.good()) {
- cout << "Error occurred at writing time!" << endl;
- return 1;
- }
- else
- cout << "\n Data Successfully written to file";
- exam.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement