All pastes #745324 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-10-22 09:23 UTC
rendered paste body
#include <iostream>
#include <fstream>
#include <iostream>
#include <memory.h>

using namespace std;

struct Date
{
    int day;
    int mounts;
    int year;
};

int sum(Date);
void print(Date);

int main()
{
    fstream f("f.bin",ios::binary|ios::in|ios::out|ios::trunc);
    int n = 0, i = 0;
    cout << "Enter number of records: "; cin >> n;
    Date *date = new Date[n];
//    memset(&date, '\0',sizeof(date));
    do
    {
        cout << "day \t= ";     cin >> date[i].day;
        cout << "mounts \t= ";  cin >> date[i].mounts;
        cout << "year \t= ";    cin >> date[i].year;
        i++;
    }
    while (i!=n);
    f.seekp(0,ios::end);
    f.write((const char*)&date,sizeof(date));

    memset(&date, '\0', sizeof(date));

    f.seekp(0,ios::beg);
    f.read((char*)&date,sizeof(date));
    fstream g("g.bin",ios::binary|ios::in|ios::trunc);
    Date tmpDate;
    memset(&tmpDate, '\0', sizeof(tmpDate));
    for (int j = 0; j < i-1 ; j++)
    {
        if (sum(date[j]) > sum(date[j+1]))
        {
            tmpDate = date[j];
            date[j] = date[j+1];
            date[j+1] = tmpDate;
        }
    }
    g.seekp(0,ios::end);
    g.write((const char*)&date, sizeof(date));

    for (int c = 0; c < i ; c++)
    {
        print(date[c]);
    }

    f.close();
    g.close();

    delete []date;

    return 0;
}

int sum(Date d)
{
    return (d.day + d.mounts + d.year);
}

void print(Date d)
{
    cout << "-----------------" <<                 endl;
    cout << "day \t= "          << d.day        << endl;
    cout << "mounts \t= "       << d.mounts     << endl;
    cout << "year \t= "         << d.year       << endl;
}