我的意思是读出一个文件里的数据,让这些数据形成链表,编译是通过了,但是运行时总是出错,是某某内存不能read.
代码如下:
ifstream intput;
CString m_text3,m_text4;
UpdateData(true);
intput.open("d:\\ok.txt",ios::in|ios::binary);
if(!intput)
{cout<<"can't open the file"<<endl;return;}
while(!intput.eof())
{intput.read((char *)&m_text3,sizeof(CString));
intput.read((char*)&m_text4,sizeof(CString));
if(head==NULL){n=m=new words;strcpy(m->word,m_text3);strcpy(m->wordanswer,m_text4);m->next=NULL;head=m;}
if(head!=NULL){m=new words;strcpy(m->word,m_text3);strcpy(m->wordanswer,m_text4);m->next=NULL;n->next=m;n=m;}
}
intput.close();
代码如下:
ifstream intput;
CString m_text3,m_text4;
UpdateData(true);
intput.open("d:\\ok.txt",ios::in|ios::binary);
if(!intput)
{cout<<"can't open the file"<<endl;return;}
while(!intput.eof())
{intput.read((char *)&m_text3,sizeof(CString));
intput.read((char*)&m_text4,sizeof(CString));
if(head==NULL){n=m=new words;strcpy(m->word,m_text3);strcpy(m->wordanswer,m_text4);m->next=NULL;head=m;}
if(head!=NULL){m=new words;strcpy(m->word,m_text3);strcpy(m->wordanswer,m_text4);m->next=NULL;n->next=m;n=m;}
}
intput.close();
=============================================
这句话好像问题很大。read的参数是char*没错,但也不能随便拿个指针来强制转换阿。
sizeof(CString)这句话太新鲜了,没见过这么写的。
应该定义一个char*,比如char *pChar = new char[MAX_LEN];
intput.read(pChar,MAX_LEN);来读取