读书人

程序在这里卡住了if(cur-gt;id==id_modf

发布时间: 2012-05-14 15:24:34 作者: rapoo

程序在这里卡住了if(cur->id==id_modfiy)//什么原因?

C/C++ code
#include <iostream>#include <string>#include <cstring>#include <fstream>using namespace std;struct table{    table():next(NULL),prev(NULL){};    table(int id_t,char* name_t,int age_t):        id(id_t),name(name_t),age(age_t),next(NULL),prev(NULL){}    int id;    char* name;    int age;    table* next;    table* prev;};void ofWrite(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* ifRead(char* file,table* head,table* tail)//读出文件函数实现{    table*memory=NULL;    ifstream iff(file);    iff.seekg(0,ios::beg);    char str[8]="";    int count=0;    int len=0;    int size=sizeof(str);    while(iff.getline(str,size,' '))//读出操作与存入链表中    {        count++;        len=strlen(str);        if(count%3==1)        {            memory = new table;            if(head==NULL&&tail==NULL)            {                head=memory;                tail=memory;                int temp=0;                int i=0;                while(str[i++]!='\0')                {                    str[i]-=48;                    temp=temp*10+str[i];                }                memory->id=temp;            }            else            {                tail->next=memory;                memory->prev=tail;                tail=memory;                int temp=0;                int i=0;                while(str[i++]!='\0')                {                    str[i]-=48;                    temp=temp*10+str[i];                }                memory->id=temp;            }        }        else if(count%3==2)        {            memory->name= new char[++len];            memmove(memory->name,str,len);        }        else if(count%3==0)        {            int temp=0;            int i=0;            while(str[i++]!='\0')            {                str[i]-=48;                temp=temp*10+str[i];            }            memory->age=temp;            count=0;        }        memset(str,0,size);    }    iff.close();    return head;}void revise(table* head,char* file)//修改完毕并存入文件中{    cout<<"input student's id that you want to midfy"<<endl;    int id_modfiy;    cin>>id_modfiy;    table* cur=head;    while(cur!=NULL)    {        if(cur->id==id_modfiy)        {            cout<<"1.Do you want to modify the student's id"<<endl                <<"2.Do you want to modify the student's name"<<endl                <<"3.Do you want to modify the student's age"<<endl                <<"4.Do you want to modify the student's whole information"                <<endl;            int choice;            cin>>choice;            switch(choice)            {                cout.flush();                case 1:cin>>cur->id;break;                case 2:cin>>cur->name;break;                case 3:cin>>cur->age;break;                case 4:cin>>cur->name>>cur->id>>cur->age;break;            }        }        cur=cur->next;    }    ofWrite(file,head);}int main(){       table* head=NULL,*tail=NULL;    char fname[30]="";    cout<<"input student's information table file name"<<endl;    cin>>fname;    head=ifRead(fname,head,tail);    revise(head,fname);    return 0;}


[解决办法]
C++中的高级货

读书人网 >C++

热点推荐