C++链表文件操作

这段代码展示了如何使用C++定义一个结构体`student`,包含学生信息如姓名、准考证号等,并实现链表的读取和保存到文件`a.txt`。`ReadLinkList`函数从文件读取数据并创建链表,空数据则删除节点;`SaveLinkList`函数将链表内容写入文件。
摘要由CSDN通过智能技术生成

结构体

typedef struct student
{
    string name;             //学生姓名
    string num;              //准考证号
    string ID;               //身份证号
    int exam_room=-1;        //考生考场
    int exam_seat=-1;        //考生座号
    int _num;
    double CET4_grades=-1;   //四级成绩
    double CET6_grades=-1;   //六级成绩
    double CET4_condition=-1;//四级报名状态
    double CET6_condition=-1;//六级报名状态
}ElemType;
/*读取存档*/
void ReadLinkList(LinkList &L)
{
    LNode *p = L;
    ifstream in("a.txt",ios::in);
    if(!in.is_open())
    {cout << "Error opening file"; exit (1); }

    while (!in.eof()){
        LNode *s = new LNode;
        in>>s->data.name>>s->data.num>>s->data.ID
        >>s->data.exam_room>>s->data.exam_seat
        >>s->data.CET4_grades>>s->data.CET6_grades
        >>s->data.CET4_condition>>s->data.CET6_condition;
        if(s->data.CET4_condition==-1&&s->data.CET6_condition==-1){
            delete s;       //若读取的数据为空,则删除s结点
        }
        else{
            s->next = NULL;
            p->next = s;
            p = s;
        }
    }
}


/*存档链表*/
void SaveLinkList(LinkList & L)
{
    ofstream  out("a.txt",ios::out);
    LNode* p;
    p=L->next;

    while(p){
        out<<p->data.name<<" "<<p->data.num<<" "<<p->data.ID<<" "
        <<p->data.exam_room<<" "<<p->data.exam_seat<<" "
        <<p->data.CET4_grades<<" "<<p->data.CET6_grades<<" "
        <<p->data.CET4_condition<<" "<<p->data.CET6_condition<<"\n";
        p=p->next;
    }
    out.close();
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱与意志

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值