首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++//文件读取数据,后怎么建立链表

2012-04-14 
C++//文件读取数据,后如何建立链表?C/C++ code#include iostream#include string#include cstring#i

C++//文件读取数据,后如何建立链表?

C/C++ code
#include <iostream>#include <string>#include <cstring>#include <fstream>using namespace std;struct table{    int id;    char name[10];    int age;    table* next;};void Create(){    table* head=NULL,*tail=NULL,*memory=NULL;    string str("Yes");    char choice[4]="";    do{        memory = new table;        cout<<"input new student on the table.txt"<<endl;        cout<<"input the student's id:"<<endl;        cin>>id;        cout<<"input the student's name:"<<endl;        cout.flush();        cin>>name;        cout<<"input the studen's age"<<endl;        cin>>age;        new -> next = NULL;        if(head==NULL&&tail==NULL)        {            head=memory;            tail=memory;        }        else        {            tail->next=memory;            tail=memory;        }        printf("choose Y to go ahead,choose N to quit:");        cout.flush();        cin>>choice;    }while(str==choice);    ofRead("table.txt",head);    delete memory;}void ofRead(char* file,table* head){    ofstream of(""file"");    of.seekp(0,ios::beg);    table* cur=head;    do{        of<<cur->id<<' '<<cur->name<<' '<<cur->age<<' ';        cur=cur->next;    }while(cur==NULL);    of.close();}table* ofWrite(char* file){    table* head=NULL,*cur=NULL,*memory=NULL;    ifstream iff(""file"");    iff.seekg(0,ios::beg);    char str[8]="";    while(iff.getline(str,sizeof(str),' '))        memory = new table;        //文件读取数据,后如何建立链表?    iff.close();    return head;}void revise(table* head){    cout<<"input student's id that you want to midfy"<<endl;    int id_modfiy;    table* cur=head;    while(cur!=NULL)    {        if(cur->id==id_modfiy)        {            cout<<"1.Do you want to modify the student's id"                <<"2.Do you want to modify the student's name"                <<"3.Do you want to modify the student's age"                <<"4.Do you want to modify the student's whole information"                endl;            int choice;            cin>>choice;            switch(choice)            (                case 1:cin>>cur->id;break;                case 2:cin>>cur->name;break;                case 3:cin>>cur->age;break;                case 4:cin>>cur->id>>cur->name>>cur->age;break;            )        }        cur=cur->next;    }    ofRead(name,head);}int main(){    Create();    cout<<"Do you want to modify the student information?(yes or no)"<<endl;    string str("yes");    char choice[4]="";    cin>>choice;    if(str==choice)    {        char name[10]="";        cout<<"input student's information table file name"<<endl;        cin>>name;        table* head=ifWrite(name);        revise(head);    }    if(strcmp(choice=="no")==0)    {        cout<<"your operaion complete."<<endl;    }}


[解决办法]
C/C++ code
#include <iostream>#include <string>#include <cstring>#include <fstream>using namespace std;struct table{    int id;    char name[10];    int age;    table* next;};void ofRead(char* file,table* head)//这儿提到前边来了{    ofstream of(file);    of.seekp(0,ios::beg);    table* cur=head;    do{        of<<cur->id<<' '<<cur->name<<' '<<cur->age<<' ';        cur=cur->next;    }while(cur==NULL);    of.close();}void Create(){    table* head=NULL,*tail=NULL,*memory=NULL;    string str("Yes");    char choice[4]="";    do{        memory = new table;        cout<<"input new student on the table.txt"<<endl;        cout<<"input the student's id:"<<endl;        cin>>memory->id;//这儿        cout<<"input the student's name:"<<endl;        cout.flush();        cin>>memory->name;//这儿        cout<<"input the studen's age"<<endl;        cin>>memory->age;//这儿        memory -> next = NULL;        if(head==NULL&&tail==NULL)        {            head=memory;            tail=memory;        }        else        {            tail->next=memory;            tail=memory;        }        printf("choose Y to go ahead,choose N to quit:");        cout.flush();        cin>>choice;    }while(str==choice);    ofRead("table.txt",head);    delete memory;}table* ifWrite(char* file)//这儿{    table* head=NULL,*cur=NULL,*memory=NULL;    ifstream iff(file);    iff.seekg(0,ios::beg);    char str[8]="";    while(iff.getline(str,sizeof(str),' '))        memory = new table;        //文件读取数据,后如何建立链表?    iff.close();    return head;}void revise(table* head){    cout<<"input student's id that you want to midfy"<<endl;    int id_modfiy;    table* cur=head;    while(cur!=NULL)    {        if(cur->id==id_modfiy)        {            cout<<"1.Do you want to modify the student's id"                <<"2.Do you want to modify the student's name"                <<"3.Do you want to modify the student's age"                <<"4.Do you want to modify the student's whole information"                <<endl;            int choice;            cin>>choice;            switch(choice)            {//这儿等等                case 1:cin>>cur->id;break;                case 2:cin>>cur->name;break;                case 3:cin>>cur->age;break;                case 4:cin>>cur->id>>cur->name>>cur->age;break;            }        }        cur=cur->next;    }    ofRead(cur->name,head);}int main(){    Create();    cout<<"Do you want to modify the student information?(yes or no)"<<endl;    string str("yes");    char choice[4]="";    cin>>choice;    if(str==choice)    {        char name[10]="";        cout<<"input student's information table file name"<<endl;        cin>>name;        table* head=ifWrite(name);        revise(head);    }    if(strcmp(choice,"no")==0)    {        cout<<"your operaion complete."<<endl;    }} 

热点排行