Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5.  
  6. struct Student {
  7. int kcstID;
  8. int quiz1;
  9. int quiz2;
  10. int quiz3;
  11. float average;
  12. };
  13.  
  14. int main() {
  15.  
  16. // Write Code to open Binary File
  17. ofstream exam ("student.dat" , ios::out | ios::binary);
  18. const int n=5 ;
  19. if(!exam) {
  20. cout << "Cannot open file" << endl;
  21. return 1;
  22. }
  23. Student stu[n] = { {163443, 8, 8, 9},
  24. {165143,5,4,6},
  25. {185421,10,8,9},
  26. {165841,9,8,10},
  27. {162315,7,8,8} };
  28. for ( int i = 0 ; i < n ; i++){
  29. stu[i].average = (stu[i].quiz1 + stu[i].quiz2 + stu[i].quiz3) / 3.0 ;
  30. exam.write((char *)&stu[i], sizeof(Student));
  31. }
  32.  
  33. // Write Code to write to file using structure in Binary format
  34.  
  35. if (!exam.good()) {
  36. cout << "Error occurred at writing time!" << endl;
  37. return 1;
  38. }
  39. else
  40. cout << "\n Data Successfully written to file";
  41. exam.close();
  42.  
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement