leoe_
2016-04-18 03:04
采纳率: 26.7%
浏览 5.8k

关于c++将文件内容读取到链表中

图片说明图片说明图片说明

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

10条回答 默认 最新

  • 小灸舞 2016-04-19 04:51
    已采纳
     #include<string>
    #include<iostream>
    #include<fstream>
    #include <stdlib.h>
    using namespace std;
    
    class Person
    {
    protected:
        string name;
        int age;
        string sex;
    };
    
    class Doctor:public Person
    {
    public:
        void show();
        void del();
        void add();
        void refer();
        void save();
        void open();
        Doctor *get();
        Doctor *find_d(int card);
    protected:
        string spe;
        double fee;
        int son_d;
    //private:
    public:
        Doctor *next,*head,*prece;
    };
    
    void Doctor::open()
    {
        FILE *fp;
        Doctor *p1,*p2;
        head=new Doctor;
        head->prece=NULL;
        p1=head;
        ifstream infile("file.txt",ios::in|ios::binary);
        p2=new Doctor;
        while(infile.read((char*)p2,sizeof(Doctor)))
        {
            cout<<p2->age;
            p1->next=p2;
            p2->prece=p1;
            p1=p2;
            cout<<"aaaa"<<endl;
            p2=new Doctor;
        }
        p1->next = NULL;
        infile.close();
    }
    void Doctor::save()
    {
        Doctor *p;
        p=head->next;
        fstream outfile("file.txt",ios::out|ios::binary);
        if(!outfile)
        {
            cout<<"打开文件失败!"<<endl;
        }
        else
        {
            while(p)
            {
                outfile.write((char *)p,sizeof(Doctor));
                p=p->next;
                cout << "haha" << endl;
            }
        }
        outfile.close();
    }
    Doctor* Doctor::find_d(int card)
    {
        int card2;
        Doctor *p;
        p=head->next;
        while(p)
        {
            card2=p->son_d;
            if(card2==card)
            {
                return p;
            }
            else
            {
                p=p->next;
            }
            if(!p)
            {
                return p=NULL;
            }
        }
    
    }
    Doctor* Doctor::get()
    {
        Doctor *p=new Doctor;
    
        cout<<" 姓名:";
        cin>>p->name;
        cout<<" 医生编号:";
        cin>>p->son_d;
        cout<<" 年龄:";
        cin>>p->age;
        cout<<" 性别:";
        cin>>p->sex;
        cout<<" 专业:";
        cin>>p->spe;
        cout<<" 诊费:";
        cin>>p->fee;
    
        return p;
    }
    
    void Doctor::add()
    {
        Doctor *p1,*p2;
        char y;
        p1=head;
        cout<<"请输入要添加的医生信息:"<<endl;
        p2=get();
    
        if(p1->next!=NULL)
            p1->next->prece=p2;
        if(p1->next==NULL)
            p2->next=NULL;
        p2->prece=p1;
        p2->next=p1->next;
        p1->next=p2;
    
    
        cout<<"医生信息添加成功!"<<endl;
        cout<<"是否继续添加医生信息?(y/n)"<<endl;
        cin>>y;
    
        if(y=='y'||y=='Y')
        {
            add();
        }
        else
        {
            return;
        }
    }
    
    
    
    void Doctor::del()
    {
        int card;
        Doctor *p;
        cout<<"请输入要删除医生信息的医生编号:";
        cin>>card;
        p=find_d(card);
    
        if(p==NULL)
        {
            cout<<"对不起!查无此人。"<<endl;
        }
        else
        {
            p->prece->next=p->next;
            if(p->next!=NULL)
                p->next->prece=p->prece;
            delete p;
            cout<<"删除成功!"<<endl;
        }
    }
    
    
    void Doctor::refer()
    {
        Doctor *p;
        cout<<"输入要查询的医生信息的医生编号:";
        int card;
        cin>>card;
        p=find_d(card);
        if(p==NULL)
        {
            cout<<"对不起!查无此人。"<<endl;
        }
        else
        {
            cout<<"姓名:";
            cout<<p->name;
            cout<<"    医生编号:";
            cout<<p->son_d;
            cout<<"    年龄:";
            cout<<p->age;
            cout<<"    性别:";
            cout<<p->sex;
            cout<<"    专业:";
            cout<<p->spe;
            cout<<"    诊费:";
            cout<<p->fee;
        }
    }
    
    
    void Doctor::show()
    {
        Doctor *p;
    
        p=head->next;
    
        while(p)
        {
            cout<<" 姓名:";
            cout<<p->name;
            cout<<"    医生编号:";
            cout<<p->son_d;
            cout<<"    年龄:";
            cout<<p->age;
            cout<<"    性别:";
            cout<<p->sex;
            cout<<"    专业:";
            cout<<p->spe;
            cout<<"    诊费:";
            cout<<p->fee;
    
            p=p->next;
            cout<<endl;
        }
    }
    
    int main()
    {
        Doctor d;
        d.head = new Doctor;
        d.head->prece = NULL;
        d.head->next = NULL;
        //d.add();
        d.open();
        d.show();
        //d.save();
    
        return 0;
    }
    
    
    已采纳该答案
    4 打赏 评论
  • leoe_ 2016-04-18 03:05

    为什么链表读取完文件的内容后却输出不出来???

    打赏 评论
  • leoe_ 2016-04-18 03:08

    ![图片说明](https://img-ask.csdn.net/upload/201604/18/1460948865_458959.png)图片说明而且我在读取中加入标示,标示读取了三次,说明读取成功了,但是为什么就是输出不出来?

    打赏 评论
  • leoe_ 2016-04-18 03:13

    图片说明

    打赏 评论
  • beifengche 2016-04-18 03:45

    你调试一下,在输出是看一下变量。

    打赏 评论
  • 不知道,为什么链表读取完文件的内容后却输出不出来???

    打赏 评论
  • 小灸舞 2016-04-18 04:51

    应该是你的read有问题
    建议贴一下Doctor这个类
    目测你Doctor类的name成员应该是个指针吧?不是一个字符数组吧,没有new过空间,直接read进去了,这样之后访问的时候肯定因为之前的越界写入而崩溃

    打赏 评论
  • leoe_ 2016-04-18 05:40

    图片说明

    打赏 评论
  • leoe_ 2016-04-18 06:23
    
    
     #include<string>
    #include<iostream>
    #include<fstream>
    #include <stdlib.h>
    using namespace std;
    
    class Person
    {
    protected:
        string name;
        int age;
        string sex;
    };
    
    class Doctor:public Person
    {
    public:
        void show();
        void del();
        void add();
        void refer();
        void save();
        void open();
        Doctor *get();
        Doctor *find_d(int card);
    protected:
        string spe;
        double fee;
        int son_d;
    private:
        Doctor *next,*head,*prece;
    };
    
    void Doctor::open()
    {
        FILE *fp;
        Doctor *p1,*p2;
        p1=head;
        ifstream infile("file.txt",ios::in|ios::binary);
        while(!infile.eof())
        {
            p2=new Doctor;
            infile.read((char*)p2,sizeof(Doctor));
            cout<<p2->age;
            p1->next=p2;
            p2->prece=p1;
            p1=p2;
            cout<<"aaaa"<<endl;
        }
        infile.close();
    }
    void Doctor::save()
    {
        Doctor *p;
        p=head->next;
        fstream outfile("file.txt",ios::out|ios::binary);
        if(!outfile)
        {
            cout<<"打开文件失败!"<<endl;
        }
        else
        {
            while(p)
            {
                outfile.write((char *)p,sizeof(Doctor));
                p=p->next;
            }
        }
        outfile.close();
    }
    Doctor* Doctor::find_d(int card)
    {
        int card2;
        Doctor *p;
        p=head->next;
        while(p)
        {
            card2=p->son_d;
            if(card2==card)
            {
                return p;
            }
            else
            {
                p=p->next;
            }
            if(!p)
            {
                return p=NULL;
            }
        }
    
    }
    Doctor* Doctor::get()
    {
        Doctor *p=new Doctor;
    
        cout<<" 姓名:";
        cin>>p->name;
        cout<<" 医生编号:";
        cin>>p->son_d;
        cout<<" 年龄:";
        cin>>p->age;
        cout<<" 性别:";
        cin>>p->sex;
        cout<<" 专业:";
        cin>>p->spe;
        cout<<" 诊费:";
        cin>>p->fee;
    
        return p;
    }
    
    void Doctor::add()
    {
        Doctor *p1,*p2;
        char y;
        p1=head;
        cout<<"请输入要添加的医生信息:"<<endl;
        p2=get();
    
        if(p1->next!=NULL)
            p1->next->prece=p2;
        if(p1->next==NULL)
            p2->next=NULL;
        p2->prece=p1;
        p2->next=p1->next;
        p1->next=p2;
    
    
        cout<<"医生信息添加成功!"<<endl;
        cout<<"是否继续添加医生信息?(y/n)"<<endl;
        cin>>y;
    
        if(y=='y'||y=='Y')
        {
            add();
        }
        else
        {
            return;
        }
    }
    
    
    
    void Doctor::del()
    {
        int card;
        Doctor *p;
        cout<<"请输入要删除医生信息的医生编号:";
        cin>>card;
        p=find_d(card);
    
        if(p==NULL)
        {
            cout<<"对不起!查无此人。"<<endl;
        }
        else
        {
            p->prece->next=p->next;
            if(p->next!=NULL)
                p->next->prece=p->prece;
            delete p;
            cout<<"删除成功!"<<endl;
        }
    }
    
    
    void Doctor::refer()
    {
        Doctor *p;
        cout<<"输入要查询的医生信息的医生编号:";
        int card;
        cin>>card;
        p=find_d(card);
        if(p==NULL)
        {
            cout<<"对不起!查无此人。"<<endl;
        }
        else
        {
            cout<<"姓名:";
            cout<<p->name;
            cout<<"    医生编号:";
            cout<<p->son_d;
            cout<<"    年龄:";
            cout<<p->age;
            cout<<"    性别:";
            cout<<p->sex;
            cout<<"    专业:";
            cout<<p->spe;
            cout<<"    诊费:";
            cout<<p->fee;
        }
    }
    
    
    void Doctor::show()
    {
        Doctor *p;
    
        p=head->next;
    
        while(p)
        {
            cout<<" 姓名:";
            cout<<p->name;
            cout<<"    医生编号:";
            cout<<p->son_d;
            cout<<"    年龄:";
            cout<<p->age;
            cout<<"    性别:";
            cout<<p->sex;
            cout<<"    专业:";
            cout<<p->spe;
            cout<<"    诊费:";
            cout<<p->fee;
    
            p=p->next;
            cout<<endl;
        }
    }
    
    int main()
    {
        Doctor d;
        //d.add();
        d.open();
        d.show();
        //d.save();
    
        return 0;
    }
    
    
    打赏 评论
  • leoe_ 2016-04-18 06:33

    图片说明

    打赏 评论

相关推荐 更多相似问题