15th Week Project one uses binary files to process student scores

Source: Internet
Author: User
/*  * Copyright (c) 2014, Yantai University School of computer  * All rights reserved.  * file name: Test.cpp  * for    : Liu Jiaqi  * finish Date: June 10, 2015  * version number: v1.0  *  * Problem Description: (1) fixed
Students, including the number, name, C + + class, high and English scores and total data members, the member functions are 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.  * Input Description: Add your own results  * program output: Final results  */#include <iostream> #include <fstream> #include <string


> #include <cstdlib> using namespace std; Class Student {public:     Student () {};     Student (int n,string na,int c,int m,int e): num (n), name (n  A), CPP (c), Math (m), 中文版 (e)     {        total=c+m+e,    }     Friend
istream& operator>> (IStream &in, Student &s);     friend ostream& operator<< (ostream&, Student&);
Private:     string name;
    int num;
    INT math;
    INT cpp;
    int 中文版;
    INT total;
}; istream& operator>> (IStream &in, Student &s) {    in>>s.num>>s.name>>
s.cpp>>s.math>>s.english;
    S.total=s.cpp+s.math+s.english;
    return in; } ostream& operator<< (ostream& out, student& s) {    out<<s.num<< "" &LT;&LT;S.N ame<< "<<s.cpp<<" "<<s.math<<" "<<s.english<<" <<s.total<<
Endl
    return out; } int main () {    Student stud[100],     int i,n,     string sname,     INT


Scpp, Smath, senglish;     Ifstream infile ("Score.dat", ios::in);  //open File as input, ASCII file     if (!infile)      //test successfully opened     {        cerr<< "OPen error! "
<<endl;
        exit (1);         for (i=0; i<100; i++)     {        infile>>stud[i]; & nbsp
 }     Infile.close ();
    ofstream outfile ("Binary_score.dat", ios::out|ios::binary);     if (!outfile)     {        cerr<< "Open error!"
<<endl;
        exit (1);         for (i=0; i<100; i++)     {        Outfile.write ((char*) &am
P;stud[i], sizeof (stud[i));
   }     cout<< "Enter your own information:";
    cin>>n>>sname>>scpp>>smath>>senglish;
    Student m (N,sname, Scpp, Smath, senglish);
    Outfile.write ((char*) &m, sizeof (m));
    Outfile.close ();
    Student s;
    Ifstream infile2 ("Binary_score.dat", ios::in|ios::binary);     if (!infilE2)     {        cerr<< "Open error!"
<<endl;
        exit (1); 
   }     while (1)     {        Infile2.read (char*) &s, sizeof (s));
        if (infile2.eof ()) break;
        cout<<s;
        infile2.close ();
    return 0;
 }


Operation Result:

Learning experience:

First use the display function, various hints error ... So I changed the overload, now realize the heavy-duty ... In order to make the private member to be accessed, change into the function form, if more points members, really do not want to change ...

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.