m0_62866716 2021-12-26 18:33 采纳率: 66.7%
浏览 66
已结题

C++读取文件的问题


 
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <fstream>
#define MAX 1000
using namespace std;
class Preson
{
public:
    // 保存联系人姓名
    void SetName(const string &name) {
        m_Name = name;
    }
    // 获取联系人姓名
    string GetName() {
        return m_Name;
    }
    // 保存联系人单位信息
    void SetPostion(const string &postion) {
        m_Postion = postion;
    }
    // 获取联系人单位信息
    string GetPostion() {
        return m_Postion;
    }
    // 保存联系人关系信息
    void SetRelation(const string &relation) {
        m_Relation = relation;
    }
    // 获取联系人关系
    string GetRelation() {
        return m_Relation;
    }
    // 保存联系人电话号码
    void SetPhone(const string &phone) {
        m_Phone = phone;
    }
    // 获取联系人电话号码
    string GetPhone() {
        return m_Phone;
    }
    // 保存联系人QQ
    void SetQQ(const string &qq) {
        m_QQ = qq;
    }
    // 获取联系人QQ
    string GetQQ() {
        return m_QQ;
    }
    // 保存联系人邮箱
    void SetEmail(const string &email) {
        m_Email = email;
    }
    // 获取联系人邮箱
    string GetEmail() {
        return m_Email;
    }
private:
    // 私有成员
    string m_Name;
    string m_Postion;
    string m_Relation;
    string m_Phone;
    string m_QQ;
    string m_Email;
};
struct Addressbooks
{
    struct Preson presonArray[MAX];    //保存的数组
    int m_Size;                  //个数
};
void ShowMenu()
{
    cout << "\t\t\t\t****************************" << endl;
    cout << "\t\t\t\t******  1:添加联系人  ******" << endl;
    cout << "\t\t\t\t******  2:显示联系人  ******" << endl;
    cout << "\t\t\t\t******  3:删除联系人  ******" << endl;
    cout << "\t\t\t\t******  4:查找联系人  ******" << endl;
    cout << "\t\t\t\t******  5:修改联系人  ******" << endl;
    cout << "\t\t\t\t******  6:清空联系人  ******" << endl;
    cout << "\t\t\t\t******  7:写入文件    ******" << endl;
    cout << "\t\t\t\t******  0:退出该系统  ******" << endl;
    cout << "\t\t\t\t****************************" << endl;
}
void addPreson(Addressbooks* abs)      //添加的函数
{
    if (abs->m_Size == MAX)   //看看是不是满了
    {
        cout << "通讯录满了,无法再输入了!" << endl;
        return;
    }
    else
    {
        //开始添加了
        string name;
        cout << "请输入姓名 : " << endl;
        cin >> name;
        abs->presonArray[abs->m_Size].SetName(name);
        cout << "请输入电话 : " << endl;
        string phone;
        cin >> phone;
        abs->presonArray[abs->m_Size].SetPhone(phone);
        cout << "请输入单位 : " << endl;
        string postion;
        cin >> postion;
        abs->presonArray[abs->m_Size].SetPostion(postion);
        cout << "请输入关系 : " << endl;
        string relation;
        cin >> relation;
        abs->presonArray[abs->m_Size].SetRelation(relation);
        cout << "请输入Email : " << endl;
        string email;
        cin >> email;
        abs->presonArray[abs->m_Size].SetEmail(email);
        cout << "请输入QQ : " << endl;
        string qq;
        cin >> qq;
        abs->presonArray[abs->m_Size].SetQQ(qq);
        abs->m_Size++;           //更新一下
        cout << "添加成功!" << endl;
        system("pause");
        system("cls");
    }
}
void showPerson(Addressbooks* abs)         //显示
{
    if (abs->m_Size == 0)   //判断是否为空
    {
        cout << "当前记录为空" << endl;
    }
    else
    {
        for (int i = 0; i < abs->m_Size; i++)
        {
            cout << "姓名: " << abs->presonArray[i].GetName() << "\t";
            cout << "  电话: " << abs->presonArray[i].GetPhone() << "\t";
            cout << "  单位: " << abs->presonArray[i].GetPostion() << "\t";
            cout << "  关系: " << abs->presonArray[i].GetRelation() << "\t";
            cout << "  Email: " << abs->presonArray[i].GetEmail() << "\t";
            cout << "  QQ: " << abs->presonArray[i].GetQQ() << endl;
        }
    }
    system("pause");
    system("cls");
}
int isExist(Addressbooks* abs, string name)  //检索联系人是否存在
{
    for (int i = 0; i < abs->m_Size; i++)
    {
        if (abs->presonArray[i].GetName() == name)
        {
            return i;
        }
    }
    return -1;
}
void deletePerson(Addressbooks* abs)           //删除
{
    cout << "请输入要删除的联系人姓名:" << endl;
    string name;
    cin >> name;
    int ret = isExist(abs, name);
    if (ret != -1)
    {
        for (int i = ret; i < abs->m_Size; i++)  //删除的操作
        {
            abs->presonArray[i] = abs->presonArray[i + 1];  //数据前移
        }
        abs->m_Size--;  //更新一下
        cout << "删除成功!" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    system("pause");
    system("cls");
}
void findPerson(Addressbooks* abs)       //查找
{
    cout << "请输入要查找的联系人姓名:" << endl;
    string name;
    cin >> name;
    int ret = isExist(abs, name);
    if (ret != -1)
    {    //打印
        cout << "姓名: " << abs->presonArray[ret].GetName() << "\t";
        cout << "  电话: " << abs->presonArray[ret].GetPhone() << "\t";
        cout << "  单位: " << abs->presonArray[ret].GetPostion() << "\t";
        cout << "  关系: " << abs->presonArray[ret].GetRelation() << "\t";
        cout << "  Email: " << abs->presonArray[ret].GetEmail() << "\t";
        cout << "  QQ: " << abs->presonArray[ret].GetQQ() << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    system("pause");
    system("cls");
}
void out(Addressbooks* abs)
{
    ofstream fp;
    fp.open("通讯录.txt", ios::out);
    fp << "姓名" << "\t" << "电话" << "\t" << "单位" << "\t" << "关系" << "\t" << "Email" << "\t" << "QQ" << endl;
    for (int i = 0; i <= abs->m_Size; i++)
    {
        fp << abs->presonArray[i].GetName() << "\t" << abs->presonArray[i].GetPhone() <<
            "\t" << abs->presonArray[i].GetPostion() << "\t" << abs->presonArray[i].GetRelation()
            << "\t" << abs->presonArray[i].GetEmail() << "\t" << abs->presonArray[i].GetQQ() << endl;
    }
    fp.close();
    cout << "写入成功" << endl;
    system("pause");
    system("cls");
}
void modifyPerson(Addressbooks* abs)           //修改
{
    cout << "请输入要修改的联系人姓名" << endl;
    string name;
    cin >> name;
    int ret = isExist(abs, name);
    if (ret != -1)
    {
        string name;
        cout << "请输入姓名 :" << endl;
        cin >> name;
        abs->presonArray[ret].SetName(name);
        cout << "请输入电话:" << endl;
        string phone;
        cin >> phone;
        abs->presonArray[ret].SetPhone(phone);
        cout << "请输入单位:" << endl;
        string postion;
        cin >> postion;
        abs->presonArray[ret].SetPostion(postion);
        cout << "请输入关系:" << endl;
        string relation;
        cin >> relation;
        abs->presonArray[ret].SetRelation(relation);
        cout << "请输入Email :" << endl;
        string email;
        cin >> email;
        abs->presonArray[ret].SetEmail(email);
        cout << "请输入QQ:" << endl;
        string qq;
        cin >> qq;
        abs->presonArray[ret].SetQQ(qq);
        cout << "修改成功!" << endl;
    }
    else
    {
        cout << "查无此人" << endl;
    }
    system("pause");
    system("cls");
}
void cleanPerson(Addressbooks* abs)
{
    abs->m_Size = 0;
    cout << "通讯录已清空!" << endl;//直接不让访问了
    system("pause");
    system("cls");
}
int main()
{
    Addressbooks abs;          //创建结构体变量
    abs.m_Size = 0;                //初始化一下
    int select = 0;  //供用户选择菜单
    while (true)
    {
        ShowMenu();
        cin >> select;
        switch (select)
        {
        case 1:                         //添加
            addPreson(&abs);        //记得带 &
            break;
        case 2:                         //显示
            showPerson(&abs);
            break;
        case 3:                         //删除
            deletePerson(&abs);
            break;
        case 4:                          //查找
            findPerson(&abs);
            break;
        case 5:                         //修改
            modifyPerson(&abs);
            break;
        case 6:                          //清空
            cleanPerson(&abs);
            break;
        case 7:
            out(&abs);
            break;
        case 0:                              //退出
            cout << "退出成功,欢迎下次使用!" << endl;
            system("pasue");
            return 0;
            break;
        default:
            break;
        }
    }
    system("pause");
    return 0;
}

要求写个读取文件的代码

  • 写回答

2条回答 默认 最新

  • _GX_ 2021-12-26 19:52
    关注

    放到main函数前,然后添加菜单"8. 读入文件",在main函数调用该函数

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月3日
  • 已采纳回答 12月26日
  • 创建了问题 12月26日

悬赏问题

  • ¥15 爬取豆瓣电影相关处理
  • ¥15 手机淘宝抓清除消息接口
  • ¥15 C#无selenium
  • ¥15 LD衰减计算的结果过大
  • ¥15 用机器学习方法帮助保险公司预测哪些是欺诈行为
  • ¥15 计算300m以内的LD衰减
  • ¥15 数据爬取,python
  • ¥15 怎么看 cst中一个面的功率分布图,请说明详细步骤。类似下图
  • ¥15 为什么我的pycharm无法用pyqt6的QtWebEngine
  • ¥15 FOR循环语句显示查询超过300S错误怎么办