二进制文件问题/随机访问

时间:2020-07-30 20:33:55

标签: c++

我需要一个函数来帮助显示使用字符数组存储在二进制文件中的记录。代码的所有其他部分均按预期工作。显示功能是菜单上的第五项,需要重新制作。数据需要存储在二进制文件中,并以随机访问模式进行访问。

P.S我不确定如何设置该功能来获取每个部门的薪水,并将其累积到报告中。我对C ++还是很陌生。

// Need to concatenate salary’s from all employees on binary files of each department and display it as a report using the displayreport() function.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

using namespace std;

const int MAX_DEPT = 3, MAX_EMPLOYEE = 7;
const int ID_SIZE = 20, NAME_SIZE = 50;

typedef struct Department
{
    //department structure
    char depID[ID_SIZE];
    char depName[NAME_SIZE];
    char depHeadName[NAME_SIZE];
    char depTotalSalary[];
} D;

typedef struct Employee
{
    //employee structure
    char employeeID[ID_SIZE];
    char employeeName[NAME_SIZE];
    double employeeSalary;
    int employeeAge;
    char employeeDepID[ID_SIZE];
} E;

void openFileDep(fstream &f)
{
    //Function to open file departments.dat
    f.open("departments.dat", ios::out|ios::in|ios::binary);

    if(f.fail())
    {
        //If file failed to open
        f.open("departments.dat", ios::out|ios::in | ios::binary| ios::trunc);
        if(f.fail())
        {
          //If file failed to open
           cout << "Error opening file....";
           exit(0);
        }
        else
          return;
    }
    else
        return;
}

void openFileEmp(fstream & z)
{
    //Function to open file employees.dat
    z.open("employees.dat", ios::out|ios::in|ios::binary);

    if(z.fail())
    {
        //If file failed to open
        z.open("employees.dat", ios::out|ios::in | ios::binary| ios::trunc);

        if(z.fail())
        {
            //If file failed to open
            cout << "Error opening file....";
            exit(0);
        }
        else
            return;
    }
    else
        return;
}

void add_newdep(fstream & f)
{
    //Function to add new record to file
    D dep;

    f.clear();//To clear the state back to good
    f.seekp(0,ios::end);//To move file pointer to end to append

    cin.ignore();

    cout<<"Enter the NEW Department Data:";
    cout<<"\nDept ID: ";

    cin.getline(dep.depID, ID_SIZE);

    cout<<"Dept Name: ";

    cin.getline(dep.depName, NAME_SIZE);

    cout<<"Head of Dept Name: ";

    cin.getline(dep.depHeadName, NAME_SIZE);

    cout<<"\n";

    f.write((char *)&dep, sizeof(dep));//Write content of structure department to file
}

void add_newemp(fstream & z)
{
    //Function to add new record to file
    E emp;

    z.clear();//To clear the state back to good
    z.seekp(0,ios::end);//To move file pointer to end to append

    cin.ignore();

    cout<<"Enter the NEW Employee Data:";

    cout<<"\nEmployee ID: ";

    cin.getline(emp.employeeID, ID_SIZE);

    cout<<"Employee Name: ";

    cin.getline(emp.employeeName, NAME_SIZE);

    cout<<"Employee Salary: $";

    cin>>emp.employeeSalary;

    cin.ignore();                     //?

    cout<<"Employee Age: ";

    cin>>emp.employeeAge;

    cout<<"Department ID: ";

    cin>>emp.employeeDepID;

    cout<<"\n";

    z.write((char *)&emp, sizeof(emp));//Write content of struct employee to file
}

void displaymodifyDep(fstream & f)
{
    //Function to display selected record and modify option
    D dep;

    f.clear();//To clear the state back to good
    f.seekg(0,ios::beg);//To move file pointer to beginning

    fstream o("temp.dat", ios::binary | ios::out);//Used to store modified records

    int n = 0;

    while(f.read((char *)&dep, sizeof(dep)))//Read whole file for number of records
        n++;

    if(n == 0)
    {
        //File is empty
        cout<<"\nNo record found. File empty";
        return;
    }
    else
    {
        //File is not empty
        cout<<"Which record to EDIT:";
        cout<<"\nPlease choose one of the following... 1 to "<<n<<" : ";

        cin>>n;//Now n is used to store record number to display

        f.clear();//To clear the state back to good
        f.seekg(0,ios::beg);//To move file pointer to beginning

        int i = 0;

        while(f.read((char *)&dep, sizeof(dep)))
        {
            //Read each record and check if i == n
            i++;

            if(i == n)
            {
                //If i == n then print that record and return
                cout<<"\nDisplay Department Details: ";
                cout<<"\nDept ID     : "<<dep.depID;                     // need to SET SPACES
                cout<<"\nDept Name   : "<<dep.depName;
                cout<<"\nDept Head   : "<<dep.depHeadName<<endl;

                cin.ignore();

                cout<<"\nEDIT the Department Data:"<<endl;
                cout<<"Dept Name: ";

                cin.getline(dep.depName, NAME_SIZE);

                cout<<"Head of Dept Name: ";

                cin.getline(dep.depHeadName, NAME_SIZE);

                cout<<"\n";

            }o.write((char *)&dep, sizeof(dep));//Write to temp.dat
        }

    }//Close both files

    f.close();
    o.close();

    remove("departments.dat");//Remove inventory.data
    rename("temp.dat", "departments.dat");//temp.data contain modified record and now has been renamed to departments.dat
}

void displaymodifyEmp(fstream & z)
{
    //Function to display selected record and modify option
    E emp;

    z.clear();//To clear the state back to good
    z.seekg(0,ios::beg);//To move file pointer to beginning

    fstream o("temp.dat", ios::binary | ios::out);//Used to store modified records

    int n = 0;

    while(z.read((char *)&emp, sizeof(emp)))//Read whole file for number of records
        n++;

    if(n == 0)
    {
        //File is empty
        cout<<"\nNo record found. File empty";
        return;
    }
    else
    {
        //File is not empty
        cout<<"Which record to EDIT:";
        cout<<"\nPlease choose one of the following... 1 to "<<n<<" : ";
        cin>>n;//Now n is used to store record number to display

        z.clear();//To clear the state back to good
        z.seekg(0,ios::beg);//To move file pointer to beginning

        int i = 0;

        while(z.read((char *)&emp, sizeof(emp)))
        {
            //Read each record and check if i == n
            i++;
            if(i == n)
           {
                //If i == n then print that record and return
                cout<<"\nDisplay Employee Details: ";
                cout<<"\nID     : "<<emp.employeeID;                     // need to SET SPACES
                cout<<"\nName   : "<<emp.employeeName;
                cout<<"\nSalary : "<<emp.employeeSalary;
                cout<<"\nAge    : "<<emp.employeeAge;
                cout<<"\nDept   : "<<emp.employeeDepID<<endl;

                cin.ignore();

                cout<<"\nEDIT the Employee Data:"<<endl;
                cout<<"Employee Name: ";

                cin.getline(emp.employeeName, NAME_SIZE);

                cout<<"Employee Salary: $";

                cin>>emp.employeeSalary;

                cin.ignore();                     //?

                cout<<"Employee Age: ";

                cin>>emp.employeeAge;

                cout<<"Department ID: ";

                cin>>emp.employeeDepID;

            }o.write((char *)&emp, sizeof(emp));//Write to temp.dat
        }
    }//Close both files

    z.close();
    o.close();

    remove("employees.dat");//Remove inventory.data
    rename("temp.dat", "employees.dat");//temp.data contain modified record and now has been renamed to employees.dat
}

//#5 THIS FUNCTION NEEDS REWORKING

void displayreport(fstream & f, fstream & z)
{
    fstream depdepName("departments.dat", ios::out | ios::in | ios::binary);
    fstream empemployeeSalary("employees.dat", ios::out | ios::in | ios::binary);

    D dep;
    E emp;
    double totaldepsalary = 0;

    f.clear();
    z.clear();

     cout << "\nSalary Report By Department \n";
     cout << "\n";
     cout << "Dept         :" << dep.depName << endl;
     cout << "Total Salary : $" <<emp.employeeSalary << endl;
     cout<<"\nSalary Report By Department \n";
}

//main starts here!
int main()
{
    //Driver function
    fstream f, z;

    openFileDep(f);//Function to open file
    openFileEmp(z);

    int option;

    do
    {
        cout<<"Human Resources Menu";
        cout<<"\n1. Create Department";
        cout<<"\n2. Create Employee";
        cout<<"\n3. Edit Department";
        cout<<"\n4. Edit Employee";
        cout<<"\n5. Display Salary Report";
        cout<<"\n6. -- Quit --";
        cout<<"\nPlease make a selection : ";

        cin>>option;

        switch(option)
        {
            case 1: add_newdep(f);break;
            case 2: add_newemp(z);break;
            case 3://display records && Modify file records
            displaymodifyDep(f);
            //close file
            f.close();
            openFileDep(f);//Open file again
            break;
            case 4://display records && Modify file records
            displaymodifyEmp(z);
            //close file
            f.close();
            openFileEmp(z);//Open file again
            break;
            case 5: displayreport(f, z);
            case 6: break;
            default: cout<<"Please enter a valid choice (1 - 6): ";
            cin>>option;
        }
    }while(option != 6);
    cout<<"Thank you, goodbye.";

    f.close();
    
    return 0;

}

0 个答案:

没有答案