首页 > 解决方案 > 我关于学生数据库管理的项目代码在编译器上完美运行,但文本文件似乎被扭曲了。问题可能是什么?

问题描述

#include<iostream>
#include<fstream>
using namespace std;
class own
{
public:
    int idnum;
    char name[50];
    int age;
    char address[50];
    long contact;
public:
     virtual void getdata()
     {
    cout<<"\nEnter The Student ID: ";
    cin>>idnum;
    cout<<"\nEnter Student's Name: ";
    cin>>name;
    cout<<"\nEnter Student's Age: ";
    cin>>age;
    cout<<"\nEnter Student's Address: ";
    cin>>address;
    cout<<"\nEnter Student's Contact Number(+880): ";
    cin>>contact;
     }
     virtual void showdata() const
     {
    cout<<"\nID Number: "<<idnum;
    cout<<"\nName: "<<name;
    cout<<"\nAge: "<<age;
    cout<<"\nAddress: "<<address;
    cout<<"\nContact Number(+880): "<<contact;
     }
     int getIDNum() const
     {
         return idnum;
     }
};
class parents : public own
{
public:
    char f_name[50];
    char m_name[50];
public:
    void getdata()
    {
      cout<<"\nEnter Student's Father's Name: ";
      cin>>f_name;
      cout<<"\nEnter Student's Mother's Name: ";
      cin>>m_name;
    }
    void showdata() const
    {
       cout<<"\nFather's Name: "<<f_name;
       cout<<"\nMother's Name: "<<m_name;
    }
};
class academic : public own
{
public:
    char university[50];
    char department[50];
    int semester;
public:
    void getdata()
    {
      cout<<"\nEnter Student's University Name: ";
      cin>>university;
      cout<<"\nEnter Student's Department: ";
      cin>>department;
      cout<<"\nEnter Student's Semester: ";
      cin>>semester;
    }
    void showdata() const
    {
       cout<<"\nUniversity Name: "<<university;
       cout<<"\nDepartment: "<<department;
       cout<<"\nSemester: "<<semester;
    }
};

void write_student();
void display_all();
void display_sp(int);
void modify_student(int);
void delete_student(int);

void write_student()
{
    own st;
    parents pt;
    academic ac;
    ofstream outFile;
    outFile.open("student.dat",ios::binary|ios::app);
    st.getdata();
    pt.getdata();
    ac.getdata();
    outFile.write(reinterpret_cast<char *> (&st), sizeof(own));
    outFile.write(reinterpret_cast<char *> (&pt), sizeof(parents));
    outFile.write(reinterpret_cast<char *> (&ac), sizeof(academic));
    outFile.close();
        cout<<"\n\nStudent record Has Been Created ";
        cin.ignore();
        cin.get();
}
void display_all()
{
    own st;
    parents pt;
    academic ac;
    ifstream inFile;
    inFile.open("student.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
    cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";
    while(inFile.read(reinterpret_cast<char *> (&st), sizeof(own))&&inFile.read(reinterpret_cast<char *> (&pt), sizeof(parents))&&inFile.read(reinterpret_cast<char *> (&ac), sizeof(academic)))
    {
        st.showdata();
        pt.showdata();
        ac.showdata();
        cout<<"\n\n====================================\n";
    }
    inFile.close();
    cin.ignore();
    cin.get();
}
void display_sp(int n)
{
    own st;
    parents pt;
    academic ac;
    ifstream inFile;
    inFile.open("student.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
    bool flag=false;
    while(inFile.read(reinterpret_cast<char *> (&st), sizeof(own))&&inFile.read(reinterpret_cast<char *> (&pt), sizeof(parents))&&inFile.read(reinterpret_cast<char *> (&ac), sizeof(academic)))
    {
        if(st.getIDNum()==n)
        {
             st.showdata();
             pt.showdata();
             ac.showdata();
             flag=true;
        }
    }
    inFile.close();
    if(flag==false)
        cout<<"\n\nrecord not exist";
    cin.ignore();
    cin.get();
}
void modify_student(int n)
{
    bool found=false;
    own st;
    parents pt;
    academic ac;
    fstream File;
    File.open("student.dat",ios::binary|ios::in|ios::out);
    if(!File)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
        while(!File.eof() && found==false)
    {

        File.read(reinterpret_cast<char *> (&st), sizeof(own))&&File.read(reinterpret_cast<char *> (&pt), sizeof(parents))&&File.read(reinterpret_cast<char *> (&ac), sizeof(academic));
        if(st.getIDNum()==n)
        {
            st.showdata();
            pt.showdata();
            ac.showdata();
            cout<<"\n\nPlease Enter The New Details of student"<<endl;
            st.getdata();
            pt.getdata();
            ac.getdata();
                int pos=(-1)*static_cast<int>(sizeof(st));
                int pos1=(-1)*static_cast<int>(sizeof(pt));
                int pos2=(-1)*static_cast<int>(sizeof(ac));
                File.seekp(pos,ios::cur);
                File.seekp(pos1,ios::cur);
                File.seekp(pos2,ios::cur);
                File.write(reinterpret_cast<char *> (&st), sizeof(own));
                File.write(reinterpret_cast<char *> (&pt), sizeof(parents));
                File.write(reinterpret_cast<char *> (&ac), sizeof(academic));
                cout<<"\n\n\t Record Updated";
                found=true;
        }
    }
    File.close();
    if(found==false)
        cout<<"\n\n Record Not Found ";
    cin.ignore();
    cin.get();
}
void delete_student(int n)
{
    own st;
    parents pt;
    academic ac;
    ifstream inFile;
    inFile.open("student.dat",ios::binary);
    if(!inFile)
    {
        cout<<"File could not be open !! Press any Key...";
        cin.ignore();
        cin.get();
        return;
    }
    ofstream outFile;
    outFile.open("Temp.dat",ios::out);
    inFile.seekg(0,ios::beg);
    while(inFile.read(reinterpret_cast<char *> (&st), sizeof(own))&&inFile.read(reinterpret_cast<char *> (&pt), sizeof(parents))&&inFile.read(reinterpret_cast<char *> (&ac), sizeof(academic)))
    {
        if(st.getIDNum()!=n)
        {
            outFile.write(reinterpret_cast<char *> (&st), sizeof(own));
            outFile.write(reinterpret_cast<char *> (&pt), sizeof(parents));
            outFile.write(reinterpret_cast<char *> (&ac), sizeof(academic));
        }
    }
    outFile.close();
    inFile.close();
    remove("student.dat");
    rename("Temp.dat","student.dat");
    cout<<"\n\n\tRecord Deleted ..";
    cin.ignore();
    cin.get();
}
int main()
{
    char ch;
    int num;
    cout.setf(ios::fixed|ios::showpoint);
    do
    {
    cout<<"\--------------------------------------------------";
    cout<<"\n\n\t1.CREATE STUDENT RECORD";
    cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORDS";
    cout<<"\n\n\t3.SEARCH STUDENT RECORD ";
    cout<<"\n\n\t4.MODIFY STUDENT RECORD";
    cout<<"\n\n\t5.DELETE STUDENT RECORD";
    cout<<"\n\n\--------------------------------------------------";
    cout<<"\n\n\tPlease Enter Your Choice (1-5): ";
    cin>>ch;
    switch(ch)
    {
    case '1':   write_student(); break;
    case '2':   display_all(); break;
    case '3':   cout<<"\n\n\tPlease Enter Student's ID: "; cin>>num;
                display_sp(num); break;
    case '4':   cout<<"\n\n\tPlease Enter Student's ID: "; cin>>num;
                modify_student(num);break;
    case '5':   cout<<"\n\n\tPlease Enter Student's ID: "; cin>>num;
                delete_student(num);break;
    default:    cout<<"\a";
    }
    }
    while(ch!='6');
    return 0;
}

这是学生数据库管理系统上的项目,其中,

  1. 我必须使用基类和派生类
  2. 我必须使用多态性(虚函数)
  3. 我必须使用文件

这段代码的主要问题是它完美地在编译器上运行,但在 File("student.txt")

如果有懂 C++ 的人解决这个问题,我将不胜感激。

这是学生数据库管理系统上的项目,其中,

  1. 我必须使用基类和派生类
  2. 我必须使用多态性(虚函数)
  3. 我必须使用文件

这段代码的主要问题是它完美地在编译器上运行,但在 File("student.txt")

如果有懂 C++ 的人解决这个问题,我将不胜感激。

标签: filereinterpret-caststatic-cast

解决方案


推荐阅读