15th Week Project One--using binary files to process student scores

Source: Internet
Author: User


Question and code:


/ *
* Copyright (c) 2015, School of Computer, Yantai University
* All rights reserved.
* File name: Project4.cpp
* Author: Wu Shengnan
* Completion date: June 21, 2015
* Version number: v1.0
*
* Problem Description:
(1) Define the student class, which includes the student number, name, C ++ class, high number and English scores and members of the total score data, the member function is determined according to needs.
(2) Read in the student's results, find the total score, and store it in an object array. The ASCII file score.dat holds the student numbers, names and C ++ classes, high numbers and English grades of 100 students.
(3) Save all the data to a binary file binary_score.dat, and finally enter your information through the keyboard and write it to the file (we are not humble, all three subjects are 100 points, and good luck at the end).
(4) In order to verify that the output file is correct, read the records in binary_score.dat one by one into the student objects and output them for viewing.
(5) View Binary File with BinaryViewer Command
* Enter description: slightly
* Program output: slightly
* /
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Student
{
private:
    int num;
    string name;
    double cpp;
    double math;
    double english;
    double total;
public:
    Student (int n = 0, string na = "0", double c = 0, double m = 0, double e = 0);
    void setvalue (int n, string na, double c, double m, double e);
    friend ostream & operator << (ostream &, Student &);
};
Student :: Student (int n, string na, double c, double m, double e)
{
    num = n;
    name = na;
    cpp = c;
    math = m;
    english = e;
    total = m + c + e;
}
void Student :: setvalue (int n, string nam, double c, double m, double e)
{
    num = n;
    name = nam;
    cpp = c;
    math = m;
    english = e;
    total = c + m + e;
}

ostream & operator << (ostream & out, Student & s)
{
    out << s.num << "" << s.name << "" << s.cpp << "" << s.math << "" << s.english << "" << s. total << endl;
    return out;
}
int main ()
{
    Student stu [50];
    int i;
    int num;
    string lname;
    double lcpp, lmath, lenglish;

    ifstream infile ("score.dat", ios :: in);
    if (! infile) // Test if it was successfully opened
    {
        cerr << "open error!" << endl;
        exit (1);
    }
    for (i = 0; i <45; i ++)
    {
        infile >> num >> lname >> lcpp >> lmath >> lenglish;
        stu [i] .setvalue (num, lname, lcpp, lmath, lenglish);
    }
    infile.close ();

    ofstream outfile ("binary_score.dat", ios :: out | ios :: binary); // Binary file
    if (! outfile)
    {
        cerr << "open error!" << endl;
        exit (1);
    }
    for (i = 0; i <45; i ++)
    {
        outfile.write ((char *) & stu [i], sizeof (stu [i])); // Note the write function
    }

    cout << "Enter your information:";
    cin >> num >> lname >> lcpp >> lmath >> lenglish;
    Student my (num, lname, lcpp, lmath, lenglish);
    outfile.write ((char *) & my, sizeof (my)); // Note the write function
    outfile.close ();

    Student stud;
    ifstream infile1 ("binary_score.dat", ios :: in | ios :: binary); // Read out the records in binary_score.dat one by one to the student object and output the view.
    if (! infile1)
    {
        cerr << "open error!" << endl;
        exit (1);
    }
    while (1)
    {
        infile1.read ((char *) & stud, sizeof (stud));
        if (infile1.eof ())
            break;
        cout << stud;
    }
    infile1.close ();
    return 0;
}

Knowledge Point Summary: The first for loop is to read data from a file and calculate the total, and the second for the output data.



Learning experience: began to look at the small Partner Program, there are doubts, after the discussion of the small Partner program can delete some unnecessary functions. The feel of the Bang Bang Da.


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.