15th Week on-machine practice project--processing students ' results with binary files

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.