基础数据库操作的C ++程序,即记录管理

时间:2018-05-27 09:44:39

标签: c++ database file c++11 ifstream

当我执行程序并选择选项编号2时,它会成功读取文件中的数据,但是当我第二次选择选项编号2时,第三次等等......没有任何内容出现,它返回到菜单。 我已经尝试了很多次,每次都会出现同样的错误。请帮忙。感谢所有提前。爱所有人。

#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

//class for database functions
class group
{
private:
    struct person {
            char flag;
            char empcode[5];
            char name[10];
            int age;
            float sal;
    }p;
public:
    fstream file,t_file;
    ifstream fin;
    ofstream fout;
    group();
    void addrec();
    void listrec();
    void modirec();
    void delrec();
    void recovrec();
};

//main function
int main()
{
    char choice;
    group g;
    do
    {
        system("cls");
        cout<<"1.Add Records"<<endl;
        cout<<"2.List Records"<<endl;
        cout<<"3.Modify Records"<<endl;
        cout<<"4.Delete A Record"<<endl;
        cout<<"5.Recover Deleted Records"<<endl;
        cout<<"6.Exit"<<endl;
        cout<<"Your choice ?: ";
        cin>>choice;
        system("cls");
        switch(choice)
        {
            case '1':
                g.addrec();
                break;
            case '2':
                g.listrec();
                break;
            case '3':
                g.modirec();
                break;
            case '4':
                g.delrec();
                break;
            case '5':
                g.recovrec();
                break;
            case '6':
                exit(0);
        }
    } while(choice!=6);

    return 0;
}

//default constructor
group::group()
{
    file.open("EMP.DAT",ios::in|ios::out|ios::binary);
    p.flag=' ';
    if(!file)
        cerr<<"Unable to open file";
}

//Add records at the end
void group::addrec()
{
    char ch;
    cout<<"Do You Want To Erase Previous Records ?: ";
    cin>>ch;
    if(ch=='y'||ch=='Y')
        remove("EMP.DAT");
    fout.open("EMP.DAT",ios::binary);
    fout.seekp(0L,ios::end);
    system("cls");
    do
    {
        cout<<"Enter Emp_Code, Name, age, Salary"<<endl;
        cin>>p.empcode>>p.name>>p.age>>p.sal;
        fout.write((char*)&p,sizeof(p));
        cout<<"Add another record ?: ";
        cin>>ch;
    } while(ch=='y'||ch=='Y');

    fout.close();
}

//list all the records
void group::listrec()
{
    int j=0;
    //position get pointer at beginning
    //fin.open("EMP.DAT",ios::binary);
    file.seekg(0L,ios::beg);
    while(file.read((char*)&p,sizeof(p)))
    {
        if(p.flag!='*')
        {
            cout<<"\nRecord N. "<<++j;
            cout<<"\nEmp Code: "<<p.empcode;
            cout<<"\nName: "<<p.name;
            cout<<"\nAge: "<<p.age;
            cout<<"\nSalary: "<<p.sal<<endl;
        }
    }
    cin.ignore();
    getchar();
}

//modifies a given record from file
void group::modirec()
{
    char code[5];
    int count=0;
    long int pos;
    cout<<"Enter Emp Code";
    cin>>code;
    file.open("EMP.DAT",ios::in|ios::out|ios::binary);
    file.seekg(0,ios::beg);
    //search an employee
    while(file.read((char*)&p,sizeof(p)))
    {
        //if record is found
        if(strcmp(p.empcode,code)==0&&p.flag==' ')
        {
            //recieve new data
            cout<<"Enter new Data: "<<endl;
            cout<<"Enter Emp_Code, Name, age, Salary"<<endl;
            cin>>p.empcode>>p.name>>p.age>>p.sal;
            //position put pointer to overwrite record
            pos=count*sizeof(p);
            file.seekp(pos,ios::beg);
            file.write((char*)&p,sizeof(p));
            file.close();
            cout<<endl<<"Press Any Key...";
            getchar();
            return;
        }
        count++;
    }
    file.close();
    cout<<endl<<"No Employee In File With Code: "<<code;
    cout<<endl<<"Press Any Key...";
    getchar();
}

//marks a record for deletion
void group::delrec()
{
    char code[5];
    long int pos;
    int count =0;
    cout<<"Enter Emp Code";
    cin>>code;
    file.open("EMP.DAT",ios::in|ios::out|ios::binary);
    //position pointer at beginning
    file.seekg(0L,ios::beg);
    //search an employee
    while(file.read((char*)&p,sizeof(p)))
    {
        //if record is found
        if(strcmp(p.empcode,code)==0&&p.flag==' ')
        {
            p.flag='*';
            //position put pointer
            pos=count*sizeof(p);
            file.seekp(pos,ios::beg);
            file.write((char*)&p,sizeof(p));
            file.close();
            return ;
        }
        count++;
    }
    cout<<endl<<"No Employee In File With Code: "<<code;
    file.close();
    cout<<endl<<"Press Any Key...";
    getchar();
}

//unmarkmarks a record
void group::recovrec()
{
    char code[5];
    long int pos;
    int count =0;
    cout<<"Enter Emp Code";
    cin>>code;
    file.open("EMP.DAT",ios::in|ios::out|ios::binary);
    //position pointer at beginning
    file.seekg(0L,ios::beg);
    //search an employee
    while(file.read((char*)&p,sizeof(p)))
    {
        //if record is found
        if(strcmp(p.empcode,code)==0&&p.flag=='*')
        {
            p.flag=' ';
            //position put pointer
            pos=count*sizeof(p);
            file.seekp(pos,ios::beg);
            file.write((char*)&p,sizeof(p));
            file.close();
            return ;
        }
        count++;
    }
    file.close();
    cout<<endl<<"No Employee In File With Code: "<<code;
    cout<<endl<<"Press Any Key...";
    getchar();
}

0 个答案:

没有答案