不知道這段代碼哪出問題了,編譯時到了stu0->next那塊就會出錯,說access violation,求各位講解一下,謝謝。
student 為一個結構體,里面加指標還有七項資料。
void List::readfile()
{
CStdioFile file1;
CString m_File_Path="studentinfo.txt";
file1.Open(m_File_Path,CFile::modeReadWrite);
CString filedata;
student *stu0=head;
student stu2;
student *stu1;
while(file1.ReadString(filedata))//讀出檔案中的內容。
{
char split[4]=" ";
CString str=filedata;
int nindex1=str.Find(split);
if(nindex1>=0)
{
stu2.name=str.Left(nindex1);
str=str.Right(str.GetLength()-nindex1-3);
}
int nindex2=str.Find(split);
if(nindex2>=0)
{
stu2.sex=str.Left(nindex2);
str=str.Right(str.GetLength()-nindex2-3);
}
int nindex3=str.Find(split);
if(nindex3>=0)
{
stu2.stunum=str.Left(nindex3);
str=str.Right(str.GetLength()-nindex3-3);
}
int nindex4=str.Find(split);
if(nindex4>=0)
{
stu2.classes=str.Left(nindex4);
str=str.Right(str.GetLength()-nindex4-3);
}
int nindex5=str.Find(split);
if(nindex5>=0)
{
stu2.academy=str.Left(nindex5);
str=str.Right(str.GetLength()-nindex5-3);
}
int nindex6=str.Find(split);
if(nindex6>=0)
{
stu2.phonenum=str.Left(nindex6);
str=str.Right(str.GetLength()-nindex6-3);
}
int nindex7=str.Find(split);
if(nindex7>=0)
{
stu2.roomnum=str.Left(nindex7);
str=str.Right(str.GetLength()-nindex7-3);
}
stu2.next=NULL;
stu1=new student(stu2);
if(head==NULL)
head=stu1;
else
stu0->next=stu1;
}
}
uj5u.com熱心網友回復:
student *stu0=head; 你的stu0可能是個空指標uj5u.com熱心網友回復:
可是不是把head賦給了stu0嗎?不是只要head有值,她就有值嗎?uj5u.com熱心網友回復:
head可能就是空指標。uj5u.com熱心網友回復:
head == NULL 了轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/114493.html
