2 leoe LEoe_ 于 2016.04.18 11:04 提问

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

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

10个回答

qq423399099
qq423399099   Ds   Rxr 2016.04.19 12: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;
}

LEoe_
LEoe_   2016.04.18 11:05

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

LEoe_
LEoe_   2016.04.18 11:08

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

LEoe_
LEoe_   2016.04.18 11:13

图片说明

beifengche
beifengche   2016.04.18 11:45

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

LEoe_
LEoe_ 是继承过来的,好像输出不出来
一年多之前 回复
LEoe_
LEoe_ 因为name
一年多之前 回复
wulijuan888
wulijuan888   2016.04.18 12:07

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

qq423399099
qq423399099   Ds   Rxr 2016.04.18 12:51

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

LEoe_
LEoe_ 额,name是继承过来的string类型的
一年多之前 回复
LEoe_
LEoe_   2016.04.18 13:40

图片说明

qq423399099
qq423399099 回复LEoe_: 楼主你的文件什么情况。。。
一年多之前 回复
LEoe_
LEoe_ 回复小灸舞好了,你看下,麻烦了
一年多之前 回复
qq423399099
qq423399099 楼主贴一下完整的代码吧,不要截图(代码放在代码标识里),顺便也贴一下文件里的内容吧
一年多之前 回复
LEoe_
LEoe_   2016.04.18 14: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;
}

qq423399099
qq423399099 你把head初始化。还不行的话文件发过来看一下
一年多之前 回复
LEoe_
LEoe_   2016.04.18 14:33

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!