(1) Define the student class, which includes the number, name, C + + class, high and English scores and total data members, the member function is determined as needed.
(2) read the student's score and find out the total score, and store it with an array of objects. The ASCII file Score.dat holds 100 students ' number, name and C + + classes, high scores, and English grades.
(3) Save all the data to a binary file Binary_score.dat, and finally input your information through the keyboard, and write to the file (I am not modest, three subjects full 100 points, the end of the good luck).
(4) To verify that the output file is correct, the records in the Binary_score.dat are read out to the student object and the output is viewed.
Code
#include <iostream> #include <fstream> #include <cstdlib> #include <cstring>using namespace std ; class Student{public:student () {}; Friend Ostream &operator<< (ostream &out,student &s); Friend IStream &operator>> (IStream &in,student &s);p rivate:int number; String name; Double Cpp,math,english;};o Stream &operator<< (ostream &out,student &s) {out<<s.number<< "\ T" <<s.name< < "\ t" <<s.cpp<< "\ T" <<s.math<< "\ T" <<s.english; return out;} IStream &operator>> (IStream &in,student &s) {in>>s.number>>s.name>>s.cpp> >s.math>>s.english; return in;} int main () {Ifstream infile; Infile.open ("Score.dat", ios::in); if (!infile) {cout<< "open,error!"; Exit (1); } Student s[101]; int i=0; while (!infile.eof ()) {infile>>s[i]; i++; } infile.close (); Ofstream outfile ("Binary_score.dat", ios::out|ios::binary); if (!outfile) {cout<< "open,error!"; Exit (1); } for (i=0;i<100;i++) {outfile.write ((char*) &s[i], sizeof (s[i])); } cin>>s[i]; Outfile.close (); FStream infile2 ("Binary_score.dat", ios::out|ios::binary); Student A; while (!infile2.eof ()) {Infile2.read ((char*) &a, sizeof (a)); if (infile2.eof ()) break; cout<<a; } infile2.close (); return 0;}
15th Week on-machine practice project--processing students ' results with binary files