weixin_54795555 2022-01-04 14:37 采纳率: 33.3%
浏览 31

保存到txt里后都是乱码该怎么解决呀?


#include<iostream>
#include<string>
#include <fstream>
#define MAX 100
using namespace std;
class person
{
public:
    int num;
    string name;
    string sex;
    int age;
};
class Teacher:virtual public person
{
public:
    string dept;
    string special;
    string title;
    void Input()
    {
        cout<<"Input num:";cin>>num;                                   //编号
        cout<<"Input name:";cin>>name;                               //姓名
        cout<<"Input sex:";cin>>sex;                                       //性别
        cout<<"Input age:";cin>>age;                                    //年龄
        cout<<"Input dept:";cin>>dept;                                 //所在系
        cout<<"Input special:";cin>>special;                          //专业
        cout<<"Input title:";cin>>title;                                    //职称
    }
    void Output()
    {
        cout<<"编号: "<<num<<"  "<<"姓名: "<<name<<"  "<<"性别: "<<sex<<"  "<<"年龄: "<<age<<
            "  "<<"所在系: "<<dept<<"  "<<"专业: "<<special<<"  "<<"职称: "<<title<<endl;
    }
};
Teacher Tea[MAX];
static int Teatop;
class TeaManager
{
public:
    bool Add();
    bool Search();
    void Show();
    bool Edit();
    bool Delete();
    bool Save();
    bool Read();
};
bool TeaManager::Add()
{
    Teacher t;
    int i,nu;
    if(Teatop==MAX) 
    {
        return false;
    }
    cout<<"请输入编号:";cin>>nu;
    for(i=0;i<Teatop;i++)
    {
        if(nu==Tea[i].num)
        {
            cout<<"已有编号,请重新输入"<<endl;
            return false;
        }
    }
    t.Input();
    Tea[Teatop]=t;
    Teatop++;
    cout<<"添加成功!"<<endl;
    return true;
}
bool TeaManager::Search()
{
    int j,n;
    cout<<"请输入编号:";cin>>n;
    for(j=0;j<Teatop;j++)
    {
        if(n==Tea[j].num) 
            break;
    }
    if(j==Teatop)
    {
        cout<<"没有找到。"<<endl;
        return false;
    }
    else
        Tea[j].Output();
    return true;
}
void TeaManager::Show()
{
    int i;
    if(Teatop==0)
    {
        cout<<"记录为空!"<<endl; 
    }
    for(i=0;i<Teatop;i++)
        Tea[i].Output();
}
bool TeaManager::Edit()
{
    Teacher t1;
    int j,n;
    cout<<"请输入要编辑的人的编号:";cin>>n;
    for(j=0;j<Teatop;j++)
    {
        if(n==Tea[j].num) break;
    }
    if(j==Teatop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    cout<<"输入修改后的信息,编号不能改:"<<endl;
    t1.Input();
    Tea[j]=t1;
    cout<<"编辑成功!"<<endl;
    return true;
}
bool TeaManager::Delete()
{
    int j,n;
    cout<<"请输入要删除的人的编号:";cin>>n;
    for(j=0;j<Teatop;j++)
    {
        if(n==Tea[j].num) 
            break;
    }
    if(j==Teatop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    for(j;j<Teatop;j++)
    {
        Tea[j]=Tea[j+1];
    }
    Teatop--;
    cout<<"删除成功!"<<endl;
    return true;
}
bool TeaManager::Save()
{
    int i;
    ofstream outfile;
    outfile.open("Tea_data.txt");   
       if(!outfile)
       {
           cout<<"open error!"<<endl; 
           return false; 
       }
       for(i=0;i<Teatop;i++)
           outfile.write((char *)&Tea[i],sizeof(Tea[i])); 
       outfile.close();   
       cout<<"保存成功!"<<endl;
       return true;
} 
bool TeaManager::Read()
{
    int i;
    ifstream infile;
    infile.open("Tea_data.txt");   
       if(!infile)
       {
           cout<<"open error!"<<endl; 
           return false; 
       }
       for(i=0;i<Teatop;i++)
           infile.read((char *)&Tea[i],sizeof(Tea[i])); 
       infile.close();  
       cout<<"读取成功!"<<endl;
       return true;
} 
void Tea_mune(TeaManager TM)
{
    int b;
    do{
        cout<<"                   教师管理"<<endl;
        cout<<"================================================="<<endl;
        cout<<"                   1.添加    "<<endl;
        cout<<"                   2.查询    "<<endl;
        cout<<"                   3.显示    "<<endl;
        cout<<"                   4.编辑    "<<endl;
        cout<<"                   5.删除    "<<endl;
        cout<<"                   6.统计    "<<endl;
        cout<<"                   7.保存    "<<endl;
        cout<<"                   8.读取    "<<endl;
        cout<<"                   0.退出    "<<endl;
        cout<<"================================================="<<endl;
        cout<<"请选择:";cin>>b;
        switch(b)
        {
        case 1:TM.Add();break;
        case 2:TM.Search();break;
        case 3:TM.Show();break;
        case 4:TM.Edit();break;
        case 5:TM.Delete();break;
        case 6:cout<<"共有教师人数:"<<Teatop<<endl;break;
        case 7:TM.Save();break;
        case 8:TM.Read();break;
        default:cout<<"\n error"<<endl;break;
        case 0:break;
        }
        cout<<"按回车键继续"<<endl;
    }while(b!=0);
}
 
class Tester:virtual public person
{
public:
    string testroom;
    string post;
    void Input()
    {
        cout<<"Input num:";cin>>num;
        cout<<"Input name:";cin>>name;
        cout<<"Input sex:";cin>>sex;
        cout<<"Input age:";cin>>age;
        cout<<"Input testroom:";cin>>testroom;
        cout<<"Input post:";cin>>post;
    }
    void Output()
    {
        cout<<"编号: "<<num<<"  "<<"姓名: "<<name<<"  "<<"性别: "<<sex<<"  "<<"年龄: "<<age<<
            "  "<<"所在实验室: "<<testroom<<"  "<<"职务: "<<post<<endl;
    }
 
};
Tester Test[MAX];
static int Testop;
class TestManager
{
public:
    bool Add();
    bool Search();
    void Show();
    bool Edit();
    bool Delete();
    bool Save();
    bool Read();
};
bool TestManager::Add()
{
    Tester t;
    int i,nu;
    if(Testop==MAX) 
    {cout<<"人数已满"<<endl;
    return false;
    }
    cout<<"请输入编号:";cin>>nu;
    for(i=0;i<Testop;i++)
    {
        if(nu==Test[i].num)
        {
            cout<<"已有编号,请重新输入"<<endl;
            return  false;
        }
    }
    t.Input();
    Test[Testop]=t;
    Testop++;
    cout<<"添加成功!"<<endl;
    return true;
}
bool TestManager::Search()
{
    int j,n;
    cout<<"请输入编号:";cin>>n;
    for(j=0;j<Testop;j++)
    {
        if(n==Test[j].num) 
            break;
    }
    if(j==Testop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    else
        Test[j].Output();
    return true;
}
void TestManager::Show()
{
    int i;
    if(Testop==0)
    {
        cout<<"记录为空!"<<endl;
    }
    for(i=0;i<Testop;i++)
        Test[i].Output();
}
bool TestManager::Edit()
{
    Tester t1;
    int j,n;
    cout<<"请输入要编辑的人的编号:";cin>>n;
    for(j=0;j<Testop;j++)
    {
        if(n==Test[j].num) 
            break;
    }
    if(j==Testop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    cout<<"输入修改后的信息,编号不能改:"<<endl;
    t1.Input();
    Test[j]=t1;
    cout<<"编辑成功!"<<endl;
    return true;
}
bool TestManager::Delete()
{
    int j,n;
    cout<<"请输入要删除的人的编号:";cin>>n;
    for(j=0;j<Testop;j++)
    {
        if(n==Test[j].num) break;
    }
    if(j==Testop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    for(j;j<Testop;j++)
    {
        Test[j]=Test[j+1];
    }
    Testop--;
    cout<<"删除成功!"<<endl;
    return true;
}
bool TestManager::Save()
{
    int i;
    ofstream outfile;
    outfile.open("Test_data.txt");   
       if(!outfile)
       {
           cout<<"open error!"<<endl; 
           return false;
       }
       for(i=0;i<Testop;i++)
           outfile.write((char *)&Test[i],sizeof(Test[i])); 
       outfile.close();   
       cout<<"保存成功!"<<endl;
       return true;
} 
bool TestManager::Read()
{
    int i;
    ifstream infile;
    infile.open("Test_data.txt");   
       if(!infile)
       {
           cout<<"open error!"<<endl; 
           return false;
       }
       for(i=0;i<Testop;i++)
           infile.read((char *)&Test[i],sizeof(Test[i])); 
       infile.close();  
       cout<<"读取成功!"<<endl;
       return true;
} 
void Test_mune(TestManager TM)
{
    int b;
    do{
        cout<<"                   实验人员管理"<<endl;
        cout<<"================================================="<<endl;
        cout<<"                   1.添加    "<<endl;
        cout<<"                   2.查询    "<<endl;
        cout<<"                   3.显示    "<<endl;
        cout<<"                   4.编辑    "<<endl;
        cout<<"                   5.删除    "<<endl;
        cout<<"                   6.统计    "<<endl;
        cout<<"                   7.保存    "<<endl;
        cout<<"                   8.读取    "<<endl;
        cout<<"                   0.退出    "<<endl;
        cout<<"================================================="<<endl;
        switch(b)
        {
        case 1:TM.Add();break;
        case 2:TM.Search();break;
        case 3:TM.Show();break;
        case 4:TM.Edit();break;
        case 5:TM.Delete();break;
        case 6:cout<<"共有实验员人数:"<<Testop<<endl;break;
        case 7:TM.Save();break;
        case 8:TM.Read();break;
        default:cout<<"\n error"<<endl;break;
        case 0:break;
        }
        cout<<"按回车键继续"<<endl;
    }while(b!=0);
}
 
class Policer:virtual public person
{
public:
    string polices;
    string post1;
    void Input()
    {
        cout<<"Input num:";cin>>num;
        cout<<"Input name:";cin>>name;
        cout<<"Input sex:";cin>>sex;
        cout<<"Input age:";cin>>age;
        cout<<"Input polices:";cin>>polices;
        cout<<"Input post1:";cin>>post1;
    }
    void Output()
    {
        cout<<"编号: "<<num<<"  "<<"姓名: "<<name<<"  "<<"性别: "<<sex<<"  "<<"年龄: "<<age<<
            "  "<<"政治面貌: "<<polices<<"  "<<"职称: "<<post1<<endl;
    }
 
};
Policer Policers[MAX];
static int Policersop;
class PolicerManager
{
public:
    bool Add();
    bool Search();
    void Show();
    bool Edit();
    bool Delete();
    bool Save();
    bool Read();
};
bool PolicerManager::Add()
{
    Policer t;
    int i,nu;
    if(Policersop==MAX) 
    {
        cout<<"人数已满"<<endl;
        return false;
    }
    cout<<"请输入编号:";cin>>nu;
    for(i=0;i<Policersop;i++)
    {
        if(nu==Policers[i].num)
        {cout<<"已有编号,请从输入"<<endl;
        return false;
        }
    }
    t.Input();
    Policers[Policersop]=t;
    Policersop++;
    cout<<"添加成功!"<<endl;
    return true;
}
bool PolicerManager::Search()
{
    int j,n;
    cout<<"请输入编号:";cin>>n;
    for(j=0;j<Policersop;j++)
    {
        if(n==Policers[j].num) 
            break;
    }
    if(j==Policersop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    else
        Policers[j].Output();
    return true;
}
void PolicerManager::Show()
{
    int i;
    if(Policersop==0)
    {
        cout<<"记录为空!"<<endl; 
    }
    for(i=0;i<Policersop;i++)
        Policers[i].Output();
}
bool PolicerManager::Edit()
{
    Policer t1;
    int j,n;
    cout<<"请输入要编辑的人的编号:";cin>>n;
    for(j=0;j<Policersop;j++)
    {
        if(n==Policers[j].num) 
            break;
    }
    if(j==Policersop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    cout<<"输入修改后的信息,编号不能改:"<<endl;
    t1.Input();
    Policers[j]=t1;
    cout<<"编辑成功!"<<endl;
    return true;
}
bool PolicerManager::Delete()
{
    int j,n;
    cout<<"请输入要删除的人的编号:";cin>>n;
    for(j=0;j<Policersop;j++)
    {
        if(n==Policers[j].num) 
            break;
    }
    if(j==Policersop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    for(j;j<Policersop;j++)
    {
        Policers[j]=Policers[j+1];
    }
    Policersop--;
    cout<<"删除成功!"<<endl;
    return true;
}
bool PolicerManager::Save()
{
    int i;
    ofstream outfile;
    outfile.open("Policers_data.txt");   
       if(!outfile)
       {
           cout<<"open error!"<<endl; 
           return false; 
       }
       for(i=0;i<Policersop;i++)
           outfile.write((char *)&Policers[i],sizeof(Policers[i])); 
       outfile.close();   
       cout<<"保存成功!"<<endl;
       return true;
} 
bool PolicerManager::Read()
{
    int i;
    ifstream infile;
    infile.open("Policers_data.txt");   
       if(!infile)
       { 
           cout<<"open error!"<<endl; 
           return false; 
       }
       for(i=0;i<Policersop;i++)
           infile.read((char *)&Policers[i],sizeof(Policers[i])); 
       infile.close();  
       cout<<"读取成功!"<<endl;
       return true;
} 
void Policers_mune(PolicerManager TM)
{
    int b;
    do{
        cout<<"                   行政人员管理"<<endl;
        cout<<"================================================="<<endl;
        cout<<"                   1.添加    "<<endl;
        cout<<"                   2.查询    "<<endl;
        cout<<"                   3.显示    "<<endl;
        cout<<"                   4.编辑    "<<endl;
        cout<<"                   5.删除    "<<endl;
        cout<<"                   6.统计    "<<endl;
        cout<<"                   7.保存    "<<endl;
        cout<<"                   8.读取    "<<endl;
        cout<<"                   0.退出    "<<endl;
        cout<<"================================================="<<endl;
        cout<<"请选择:";cin>>b;
        switch(b)
        {
        case 1:TM.Add();break;
        case 2:TM.Search();break;
        case 3:TM.Show();break;
        case 4:TM.Edit();break;
        case 5:TM.Delete();break;
        case 6:cout<<"共有行政员人数:"<<Policersop<<endl;break;
        case 7:TM.Save();break;
        case 8:TM.Read();break;
        default:cout<<"\n error"<<endl;break;
        case 0:break;
        }
        cout<<"按回车键继续"<<endl;
    }while(b!=0);
}
class TeacherAndPolicer: public Teacher,public Policer
{
public:
    void Input1()
    {
        cout<<"Input num:";cin>>num;
        cout<<"Input name:";cin>>name;
        cout<<"Input sex:";cin>>sex;
        cout<<"Input age:";cin>>age;
        cout<<"Input dept:";cin>>dept;
        cout<<"Input special:";cin>>special;
        cout<<"Input title:";cin>>title;
        cout<<"Input polices:";cin>>polices;
        cout<<"Input post1:";cin>>post1;
    }
    void Output1()
    {
        cout<<"编号: "<<num<<"  "<<"姓名: "<<name<<"  "<<"性别: "<<sex<<"  "<<"年龄: "<<age<<
            "  "<<"所在系: "<<dept<<"  "<<"专业: "<<special<<"  "<<"职称: "<<title<<"  "<<
            "政治面貌: "<<polices<<"  "<<"职称: "<<post1<<endl;
    }
};
TeacherAndPolicer T[MAX];
static int Ttop;
class TandPManager
{
public:
    bool Add();
    bool Search();
    void Show();
    bool Edit();
    bool Delete();
    bool Save();
    bool Read();
};
bool TandPManager::Add()
{
    TeacherAndPolicer t;
    int i,nu;
    if(Ttop==MAX) 
    {
        return false;
    }
    cout<<"请输入编号:";cin>>nu;
    for(i=0;i<Ttop;i++)
    {
        if(nu==T[i].num)
        {
            cout<<"已有编号,请重新输入"<<endl;
            return false;
        }
    }
    t.Input1();
    T[Ttop]=t;
    Ttop++;
    cout<<"添加成功!"<<endl;
    return true;
}
bool TandPManager::Search()
{
    int j,n;
    cout<<"请输入编号:";cin>>n;
    for(j=0;j<Ttop;j++)
    {
        if(n==T[j].num) 
            break;
    }
    if(j==Ttop)
    {
        cout<<"没有找到。"<<endl;
        return false;
    }
    else
        T[j].Output1();
    return true;
}
void TandPManager::Show()
{
    int i;
    if(Ttop==0)
    {
        cout<<"记录为空!"<<endl; 
    }
    for(i=0;i<Ttop;i++)
        T[i].Output1();
}
bool TandPManager::Edit()
{
    TeacherAndPolicer t1;
    int j,n;
    cout<<"请输入要编辑的人的编号:";cin>>n;
    for(j=0;j<Ttop;j++)
    {
        if(n==T[j].num) break;
    }
    if(j==Ttop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    cout<<"输入修改后的信息,编号不能改:"<<endl;
    t1.Input1();
    T[j]=t1;
    cout<<"编辑成功!"<<endl;
    return true;
}
bool TandPManager::Delete()
{
    int j,n;
    cout<<"请输入要删除的人的编号:";cin>>n;
    for(j=0;j<Ttop;j++)
    {
        if(n==T[j].num) 
            break;
    }
    if(j==Ttop)
    {
        cout<<"没有此人!"<<endl;
        return false;
    }
    for(j;j<Ttop;j++)
    {
        T[j]=T[j+1];
    }
    Ttop--;
    cout<<"删除成功!"<<endl;
    return true;
}
bool TandPManager::Save()
{
    int i;
    ofstream outfile;
    outfile.open("TeaandPol_data.txt");   
       if(!outfile)
       {
           cout<<"open error!"<<endl; 
           return false; 
       }
       for(i=0;i<Ttop;i++)
           outfile.write((char *)&T[i],sizeof(T[i])); 
       outfile.close();   
       cout<<"保存成功!"<<endl;
       return true;
} 
bool TandPManager::Read()
{
    int i;
    ifstream infile;
    infile.open("TeaandPol_data.txt");   
       if(!infile)
       {
           cout<<"open error!"<<endl; 
           return false; 
       }
       for(i=0;i<Ttop;i++)
           infile.read((char *)&T[i],sizeof(T[i])); 
       infile.close();  
       cout<<"读取成功!"<<endl;
       return true;
} 
void TeaandPol_mune(TandPManager TM)
{
    int b;
    do{
        cout<<"                   教师兼行政人员管理"<<endl;
        cout<<"================================================="<<endl;
        cout<<"                   1.添加    "<<endl;
        cout<<"                   2.查询    "<<endl;
        cout<<"                   3.显示    "<<endl;
        cout<<"                   4.编辑    "<<endl;
        cout<<"                   5.删除    "<<endl;
        cout<<"                   6.统计    "<<endl;
        cout<<"                   7.保存    "<<endl;
        cout<<"                   8.读取    "<<endl;
        cout<<"                   0.退出    "<<endl;
        cout<<"================================================="<<endl;
        cout<<"请选择:";cin>>b;
        switch(b)
        {
        case 1:TM.Add();break;
        case 2:TM.Search();break;
        case 3:TM.Show();break;
        case 4:TM.Edit();break;
        case 5:TM.Delete();break;
        case 6:cout<<"共有教师人数:"<<Teatop<<endl;break;
        case 7:TM.Save();break;
        case 8:TM.Read();break;
        default:cout<<"\n error"<<endl;break;
        case 0:break;
        }
        cout<<"按回车键继续"<<endl;
    }while(b!=0);
}
 
int main()
{
    TeaManager Tmer1;
    TestManager Tetmer;
    PolicerManager Polimer;
    TandPManager TandP;
    int a=1;
    while(a)
    {
        cout<<endl;
        cout<<"              ****欢迎使用高校人员信息管理系统****"<<endl;
        cout<<"               ================================="<<endl;
        cout<<"               | 1.教师管理                    |"<<endl;
        cout<<"               | 2.实验员管理                  |"<<endl;
        cout<<"               | 3.行政员管理                  |"<<endl;
        cout<<"               | 4.教师兼行政员管理            |"<<endl;
        cout<<"               | 0.退出                        |"<<endl;
        cout<<"               ================================="<<endl;
        cout<<"请选择:";cin>>a;    
 
        switch(a)
        {
        case 1:Tea_mune(Tmer1);break;
        case 2:Test_mune(Tetmer);break;
        case 3:Policers_mune(Polimer);break;
        case 4:TeaandPol_mune(TandP);break;
        case 0:break;
        default:cout<<"\n error"<<endl;
        cout<<"按回车键继续"<<endl;         
        break;
        }
    }
    cout<<endl<<"谢谢使用"<<endl;
    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • 於黾 2022-01-04 14:40
    关注

    因为你往里面直接存的二进制数据呀
    你想存字符串,要先把数据都转成字符串类型再写入

    评论

报告相同问题?

问题事件

  • 创建了问题 1月4日

悬赏问题

  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误